在编写应用的时候,为了调试程序方便,可以在异常处理代码中把异常信息显示出来,这样可以根据错误提示调试代码。异常处理代码通常可以写成这样:
try{
…
}catch(Exception e){
(());
}
下面是一些常见的错误信息: (1)驱动程序不存在 提示的错误信息如下: : 后半部分是您在程序中写的驱动程序的名字。 解决方法:仔细检查类名是否写错,如果类名没有写错,则是驱动程序所在的压缩包没有引入工程,想办法引入。 (2)URL写错 提示的错误信息如下: : No suitable driver 解决方法:仔细检查URL的格式是否正确,不同数据库的URL格式不同。 (3)主机IP地址不正确或者网络不通 提示的错误信息如下: : Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** MESSAGE: Connection timed out: connect STACKTRACE: : Connection timed out: connect at (Native Method) at (Unknown Source) at (Unknown Source) at (Unknown Source) at (Unknown Source) at (Unknown Source) at (Unknown Source) at .(Unknown Source) at .(Unknown Source) at (:132) at .(:273) at (:1639) at .(:393) at (:262) at (Unknown Source) at (Unknown Source) at (:25) at (:12) ** END NESTED EXCEPTION ** 解决方法:查看IP地址是否正确,网络是否有问题。 (4)端口错误或者数据库服务器没有启动 提示的错误信息如下: : Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** MESSAGE: Connection refused: connect STACKTRACE: : Connection refused: connect at (Native Method) at (Unknown Source) at (Unknown Source) at (Unknown Source) at (Unknown Source) at (Unknown Source) at (Unknown Source) at .(Unknown Source) at .(Unknown Source) at (:132) at .(:273) at (:1639) at .(:393) at (:262) at (Unknown Source) at (Unknown Source) at (:25) at (:12) ** END NESTED EXCEPTION ** 解决方法:查看端口是否正确,数据库服务器是否启动。 (5)数据库名字不正确 提示的错误信息如下: : Unknown database 'test2' 解决方法:查看数据库是否存在。 (6)用户名或者口令不正确 提示的错误信息如下: : Access denied for user 'roo'@'localhost' (using password: YES) 解决方法:确认用户名和口令是否正确。 (7)表名错误 提示的错误信息如下: : Table 'test.student1' doesn't exist 解决方法:查看表是否存在,表名是否写错。 (8)列名错误 提示的错误信息如下: : Unknown column 'sid' in 'field list' 解决方案:仔细查看数据库表中的列名。 (9)处理结果集的时候,要获取的列的序号大于列数,或者小于0 提示的错误信息如下: : Column Index out of range, 4 > 3. 这个错误产生的背景是:数据库表中只有3列,而在取信息的时候使用了(4),尤其是在使用循环处理的时候容易犯这样的错误。 (10)执行insert语句的时候,表中的列数与插入语句中的不相同 提示的错误信息如下: : Column count doesn't match value count at row 1 该错误产生的背景是:数据库表中有3列,而插入的时候给了4个值,SQL语句如下: insert into student values('0011323','李旭',22,99)。 (11)执行insert语句的时候,主键重复。 提示的错误信息如下: : Duplicate entry '0011323' for key 1 该错误产生的背景是:1条SQL语句连续执行了两遍,第二遍就出错了。 (12)执行insert语句的时候,值过长 提示的错误信息如下: : Data too long for column 'id' at row 1 解决方案,查看数据库中列的长度。