Database: MySQL
When writing an application, for the convenience of debugging the program, the exception information can be displayed in the exception handling code, so that the code can be debugged according to the error prompts. Exception handling code can usually be written like this:
try{
…
}catch(Exception e){
System.out.println(e.toString());
}
Here are some common error messages:
(1) The error message prompted when the driver does not exist 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 carefully whether the class name is written incorrectly. If the class name is not written incorrectly, it means that the compressed package where the driver is located has not been introduced into the project. Find a way to introduce it.
(2) The error message prompted when the URL is written incorrectly is as follows:
java.sql.SQLException: No suitable driver
Solution: Check carefully whether the format of the URL is correct. The URL formats of different databases are different.
(3) The error message prompted when the host IP address is incorrect or the network is unavailable 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 there is a problem with the network.
(4) The error message if the port is wrong or the database server is not started 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) The error message prompted when the database name is incorrect is as follows:
java.sql.SQLException: Unknown database 'test2'
Solution: Check whether the database exists.
(6) The error message prompted when the user name or password is incorrect is as follows:
java.sql.SQLException: Access denied for user 'roo'@'localhost' (using password: YES)
Solution: Confirm that the username and password are correct.
(7) The error message prompted by the table name error 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) The error message prompted by the column name error is as follows:
java.sql.SQLException: Unknown column 'sid' in 'field list'
Solution: Take a closer look at 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 the number of columns, or less than 0
The error message prompted is as follows:
java.sql.SQLException: Column Index out of range, 4 > 3.
The background of this error is that there are only 3 columns in the database table, and rs.getString(4) is used when fetching information. This error is easy to make, especially when using loop processing.
(10) When executing the insert statement, if the number of columns in the table is different from that in the insert statement, the error message prompted is as follows:
java.sql.SQLException: Column count doesn't match value count at row 1
The background of this error is: there are 3 columns in the database table, and 4 values were given when inserting. The SQL statement is as follows:
insert into student values('0011323','Li Xu',22,99).
(11) When executing the insert statement, the primary key is repeated.
The error message prompted is as follows:
java.sql.SQLException: Duplicate entry '0011323' for key 1
The background of this error is: a SQL statement was executed twice consecutively, and an error occurred the second time.
(12) When executing the insert statement, the error message if the value is too long is as follows:
java.sql.SQLException: Data too long for column 'id' at row 1
Solution, check the length of the column in the database.