日期:2014-05-16  浏览次数:20440 次

JDBC常见错误及调试

数据库:MySQL

在编写应用的时候,为了调试程序方便,可以在异常处理代码中把异常信息显示出来,这样可以根据错误提示调试代码。异常处理代码通常可以写成这样:

try{

}catch(Exception e){

?? System.out.println(e.toString());

}

下面是一些常见的错误信息:

(1)驱动程序不存在

提示的错误信息如下:

java.lang.ClassNotFoundException: com.mysql.jdbc.Drive

后半部分是您在程序中写的驱动程序的名字。

解决方法:仔细检查类名是否写错,如果类名没有写错,则是驱动程序所在的压缩包没有引入工程,想办法引入。

(2)URL写错

提示的错误信息如下:

java.sql.SQLException: No suitable driver

解决方法:仔细检查URL的格式是否正确,不同数据库的URL格式不同。

(3)主机IP地址不正确或者网络不通

提示的错误信息如下:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

?

** BEGIN NESTED EXCEPTION **

?

java.net.ConnectException

MESSAGE: Connection timed out: connect

?

STACKTRACE:

?

java.net.ConnectException: Connection timed out: connect

?????? at java.net.PlainSocketImpl.socketConnect(Native Method)

?????? at java.net.PlainSocketImpl.doConnect(Unknown Source)

?????? at java.net.PlainSocketImpl.connectToAddress(Unknown Source)

?????? at java.net.PlainSocketImpl.connect(Unknown Source)

?????? at java.net.SocksSocketImpl.connect(Unknown Source)

?????? at java.net.Socket.connect(Unknown Source)

?????? at java.net.Socket.connect(Unknown Source)

?????? at java.net.Socket.<init>(Unknown Source)

?????? at java.net.Socket.<init>(Unknown Source)

?????? at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:132)

?????? at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:273)

?????? at com.mysql.jdbc.Connection.createNewIO(Connection.java:1639)

?????? at com.mysql.jdbc.Connection.<init>(Connection.java:393)

?????? at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:262)

?????? at java.sql.DriverManager.getConnection(Unknown Source)

?????? at java.sql.DriverManager.getConnection(Unknown Source)

?????? at test.JDBCTest.oracleTest(JDBCTest.java:25)

?????? at test.JDBCTest.main(JDBCTest.java:12)

** END NESTED EXCEPTION **

解决方法:查看IP地址是否正确,网络是否有问题。

(4)端口错误或者数据库服务器没有启动

提示的错误信息如下:

com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

?

** BEGIN NESTED EXCEPTION **

?

java.net.ConnectException

MESSAGE: Connection refused: connect

?

STACKTRACE:

?

java.net.ConnectException: Connection refused: connect

?????? at java.net.PlainSocketImpl.socketConnect(Native Method)

?????? at java.net.PlainSocketImpl.doConnect(Unknown Source)

?????? at java.net.PlainSocketImpl.connectToAddress(Unknown Source)

?????? at java.net.PlainSocketImpl.connect(Unknown Source)

?????? at java.net.SocksSocketImpl.connect(Unknown Source)

?????? at java.net.Socket.connect(Unknown Source)

?????? at java.net.Socket.connect(Unknown Source)

?????? at java.net.Socket.<init>(Unknown Source)

?????? at java.net.Socket.<init>(Unknown Source)

?????? at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:132)

?????? at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:273)

?????? at com.mysql.jdbc.Connection.createNewIO(Connection.java:1639)

?????? at com.mysql.jdbc.Connection.<init>(Connection.java:393)

?????? at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:262)

?????? at java.sql.DriverManager.getConnection(Unknown Source)

?????? at java.sql.DriverManager.getConnection(Unknown Source)

?????? at test.JDBCTest.oracleTest(JDBCTest.java:25)

?????? at test.JDBCTest.main(JDBCTest.java:12)

?

?

** END NES