Common Errors and debugging methods of JDBC programs and common jdbc errors

Source: Internet
Author: User

Common Errors and debugging methods of JDBC programs and common jdbc errors
Database: MySQL

When writing an application, you can display the exception information in the exception handling code for the convenience of debugging the program, so that you can debug the code according to the error prompt. The exception handling code can be written as follows:
Try {
...
} Catch (Exception e ){
System. out. println (e. toString ());
}
The following are some common error messages:
(1) The driver does not exist.
The error message is as follows:
Java. lang. ClassNotFoundException: com. mysql. jdbc. Drive
The second half is the name of the driver you wrote in the program.
Solution: Check whether the class name is wrong. If the class name is not wrong, the compressed package of the driver is not introduced into the project.
(2) URL Error
The error message is as follows:
Java. SQL. SQLException: No suitable driver
Solution: Check whether the URL format is correct. The URL formats of different databases are different.
(3) Incorrect Host IP address or network connection failure
The error message is as follows:
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 **
Solution: Check whether the IP address is correct and whether the network is faulty.
(4) port error or database server not started
The error message is as follows:
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 nested exception **
Solution: Check whether the port is correct and whether the database server is started.
(5) Incorrect Database Name
The error message is as follows:
Java. SQL. SQLException: Unknown database 'test2'
Solution: Check whether the database exists.
(6) incorrect user name or password
The error message is as follows:
Java. SQL. SQLException: Access denied for user 'roo' @ 'localhost' (using password: YES)
Solution: confirm that the user name and password are correct.
(7) Table Name Error
The error message is as follows:
Java. SQL. SQLException: Table 'test. student1 'doesn' t exist
Solution: Check whether the table exists and whether the table name is written incorrectly.
(8) column name Error
The error message is as follows:
Java. SQL. SQLException: Unknown column 'sid 'in 'field list'
Solution: carefully check the column names in the database table.
(9) when processing the result set, the sequence number of the column to be obtained is greater than or less than 0.
The error message is as follows:
Java. SQL. SQLException: Column Index out of range, 4> 3.
This error occurs because there are only three columns in the database table, and rs is used for retrieving information. getString (4), especially when loop processing is used.
(10) When executing the insert statement, the number of columns in the table is different from that in the insert statement.
The error message is as follows:
Java. SQL. SQLException: Column count doesn't match value count at row 1
The background of this error is: There are three columns in the database table, and four values are given during insertion. The SQL statement is as follows:
Insert into student values ('123456', 'Li xu', 0011323 ).
(11) when executing the insert statement, the primary key is repeated.
The error message is as follows:
Java. SQL. SQLException: Duplicate entry '000000' for key 1
The cause of this error is that one SQL statement is executed twice in a row and the second SQL statement fails.
(12) when executing the insert statement, the value is too long
The error message is as follows:
Java. SQL. SQLException: Data too long for column 'id' at row 1
Solution: view the length of columns in the database.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.