JDBC programming errors and Solutions

Source: Internet
Author: User

JDBC programming errors and Solutions
Errors During JDBC programming and Solutions

Errors During JDBC programming and Solutions

Source code:

1. java. lang. ClassNotFoundException: com. microsoft. jdbc. sqlserver. SQLServerDriver

1.1 error message:

1.2 error description

1.3 error Solution

2. User 'sa 'Login Failed

2.1 error message:

2.2 error description

2.3 error Solution

3. Invalid parameter binding (s)

3.1 error message:

3.2 error description

3.3 error Solution

4. The object name 'jspbook' is invalid.

4.1 error message:

4.2 error description

4.3 error Solution

5. The column name 'bookid' is invalid.

5.1 error message:

5.2 error description

5.3 error Solution

6. Value can not be converted to requested type

6.1 error message:

6.2 error description

6.3 error Solution

 

Source code:

Package edu. ccniit. jspCourse. db. prim; import java. SQL. driverManager; import java. SQL. SQLException; /*** get the information of the book with the specified ID * @ author Administrator **/public class GetBookByID {/*** @ param args * @ throws ClassNotFoundException * @ throws SQLException */public static void main (String [] args) throws ClassNotFoundException, SQLException {Class. forName ("com. microsoft. jdbc. sqlserver. SQLServerDriver ");//------- ---------------------------------------------------------------------------------- String dbUrl = "jdbc: microsoft: sqlserver: // localhost: 1433"; java. SQL. connection con = DriverManager. getConnection (dbUrl, "sa", "123"); // optional String sqlStr = "select * from jspBook where bookId =? "; Java. SQL. preparedStatement pst = con. prepareStatement (sqlStr); // specify pst. setInt (3, 3); // parse java. SQL. resultSet rs = pst.exe cuteQuery (); // while (rs. next () {String book_id = rs. getString ("bookId"); String book_name = rs. getString ("book_name"); System. out. println (book_id + "" + book_name);} pst. close (); con. close ();}}

The following error is reported:
1. java. lang. ClassNotFoundException: com. microsoft. jdbc. sqlserver. SQLServerDriver1.1 error message:

Exception in thread "main" java. lang. ClassNotFoundException: com. microsoft. jdbc. sqlserver. SQLServerDriver

At java.net. URLClassLoader $ 1.run( Unknown Source)

At java. security. AccessController. doPrivileged (Native Method)

At java.net. URLClassLoader. findClass (Unknown Source)

At java. lang. ClassLoader. loadClass (Unknown Source)

At sun. misc. Launcher $ AppClassLoader. loadClass (Unknown Source)

At java. lang. ClassLoader. loadClass (Unknown Source)

At java. lang. ClassLoader. loadClassInternal (Unknown Source)

At java. lang. Class. forName0 (Native Method)

At java. lang. Class. forName (Unknown Source)

At edu. ccniit. jspCourse. db. prim. GetBookByID. main (GetBookByID. java: 23)


1.2 error description

Error message: the JDBC Driver Class com. microsoft. jdbc. SQLServer. SQLServerDriver of sqlserver cannot be found.

Cause of the error: the three driver files of SQLServer are not put in the build path of the project.

1.3 error Solution

Put the three driver files of SQLServer into the build path of the project. The procedure is as follows:

Step 1: Create a new directory named lib in the project. The operation sequence is as follows:

Right-click the project name and choose "New"> "folder ".

Step 2: copy the three database-driven files to the lib directory. The operation sequence is

Copy the three files obtained from the database-> select the lib directory in the project and right-click it-> select and paste

Step 3: Right-click the project name and choose build path> Configure build path> select database

The following page is displayed:

 

Step 4: Click the Add button, select the three files in the lib directory under the jspCourseExample project, and click OK. The interface is as follows:


2. User 'sa 'logon Failure error 2.1:

Exception in thread "main" java. SQL. SQLException: [Microsoft] [SQLServer 2000 Driver for JDBC] [SQLServer] user 'sa' Login Failed.

At com. microsoft. jdbc. base. BaseExceptions. createException (Unknown Source)

At com. microsoft. jdbc. base. BaseExceptions. getException (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSRequest. processErrorToken (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSRequest. processReplyToken (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSLoginRequest. processReplyToken (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSRequest. processReply (Unknown Source)

At com. microsoft. jdbc. sqlserver. SQLServerImplConnection. open (Unknown Source)

At com. microsoft. jdbc. base. BaseConnection. getNewImplConnection (Unknown Source)

At com. microsoft. jdbc. base. BaseConnection. open (Unknown Source)

At com. microsoft. jdbc. base. BaseDriver. connect (Unknown Source)

At java. SQL. DriverManager. getConnection (Unknown Source)

At java. SQL. DriverManager. getConnection (Unknown Source)

At edu. ccniit. jspCourse. db. prim. GetBookByID. main (GetBookByID. java: 26)

 

2.2 error description

Error message: you cannot log on to the SQLServer database using the sa username and 123 password.

2.3 error Solution

Enter the correct database password.

Modify the sa user password in the security options in Enterprise Manager of SQLServer.

 

 

3. error message of Invalid parameter binding (s) 3.1:

Exception in thread "main" java. SQL. SQLException: [Microsoft] [SQLServer 2000 Driver for JDBC] Invalid parameter binding (s ).

At com. microsoft. jdbc. base. BaseExceptions. createException (Unknown Source)

At com. microsoft. jdbc. base. BaseExceptions. getException (Unknown Source)

At com. microsoft. jdbc. base. BasePreparedStatement. validateParameterIndex (Unknown Source)

At com. microsoft. jdbc. base. BasePreparedStatement. setObjectInternal (Unknown Source)

At com. microsoft. jdbc. base. BasePreparedStatement. setInt (Unknown Source)

At edu. ccniit. jspCourse. db. prim. GetBookByID. main (GetBookByID. java: 32)

 

3.2 error description

Error message: Question mark (?) in the SQL statement? An error occurs when the actual data is specified? The sequence number is specified incorrectly. in SQL? The sequence number starts from 1. In addition, the value type of the specified actual data must be the Data Type of the corresponding column in the database, and must be consistent with pst. set ××× (...) The types specified by XXX are the same.

3.3 error Solution

Change pst. setInt (3, 3); Code to pst. setInt (3, 3 );

 

 

 

4. The error message 4.1 is invalid for the object name 'jspbook:

Exception in thread "main" java. SQL. SQLException: [Microsoft] [SQLServer 2000 Driver for JDBC] [SQLServer] The Object Name 'jspbook' is invalid.

At com. microsoft. jdbc. base. BaseExceptions. createException (Unknown Source)

At com. microsoft. jdbc. base. BaseExceptions. getException (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSRequest. processErrorToken (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSRequest. processReplyToken (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSRPCRequest. processReplyToken (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSRequest. processReply (Unknown Source)

At com. microsoft. jdbc. sqlserver. SQLServerImplStatement. getNextResultType (Unknown Source)

At com. microsoft. jdbc. base. BaseStatement. commonTransitionToState (Unknown Source)

At com. microsoft. jdbc. base. BaseStatement. postImplExecute (Unknown Source)

At com. microsoft. jdbc. base. BasePreparedStatement. postImplExecute (Unknown Source)

At com. microsoft. jdbc. base. BaseStatement. commonExecute (Unknown Source)

At com.microsoft.jdbc.base.BaseStatement.exe cuteQueryInternal (Unknown Source)

At com.microsoft.jdbc.base.BasePreparedStatement.exe cuteQuery (Unknown Source)

At edu. ccniit. jspCourse. db. prim. GetBookByID. main (GetBookByID. java: 34)

4.2 error description

Error message: 34 lines of code "select * from jspBook where bookId =? ", JspBook represents the table name, that is, the table in the database is not called jspBook.

4.3 error Solution

Step 1: Make sure that the database URL must have the databaseName parameter; otherwise, JDBC

The connected database is the master database, but the master database is not the database we want to operate on.

Step 2: After the databaseName parameter is specified, check whether the table named jspBook in the database is used. Change jspBook to the correct table name.

String sqlStr = "select * from jspBook where bookId =? "; Code change

String sqlStr = "select * from book where bookId =? ";

 

5. The column name 'bookid' is invalid. Error 5.1:

Exception in thread "main" java. SQL. SQLException: [Microsoft] [SQLServer 2000 Driver for JDBC] [SQLServer] The column name 'bookid' is invalid.

At com. microsoft. jdbc. base. BaseExceptions. createException (Unknown Source)

At com. microsoft. jdbc. base. BaseExceptions. getException (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSRequest. processErrorToken (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSRequest. processReplyToken (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSRPCRequest. processReplyToken (Unknown Source)

At com. microsoft. jdbc. sqlserver. tds. TDSRequest. processReply (Unknown Source)

At com. microsoft. jdbc. sqlserver. SQLServerImplStatement. getNextResultType (Unknown Source)

At com. microsoft. jdbc. base. BaseStatement. commonTransitionToState (Unknown Source)

At com. microsoft. jdbc. base. BaseStatement. postImplExecute (Unknown Source)

At com. microsoft. jdbc. base. BasePreparedStatement. postImplExecute (Unknown Source)

At com. microsoft. jdbc. base. BaseStatement. commonExecute (Unknown Source)

At com.microsoft.jdbc.base.BaseStatement.exe cuteQueryInternal (Unknown Source)

At com.microsoft.jdbc.base.BasePreparedStatement.exe cuteQuery (Unknown Source)

At edu. ccniit. jspCourse. db. prim. GetBookByID. main (GetBookByID. java: 34)

5.2 error description

Error message: 34 lines of code will use "select * from book where bookId =? "Query the database, where bookId represents the column name, but there is no column named bookId in the book table.

5.3 error Solution

Change jspBook to the correct table name.

String sqlStr = "select * from book where bookId =? "; Code change

String sqlStr = "select * from book where book_Id =? ";

 

6. Value can not be converted to requested type6.1 error message:

Exception in thread "main" java. SQL. SQLException: [Microsoft] [SQLServer 2000 Driver for JDBC] Value can not be converted to requested type.

At com. microsoft. jdbc. base. BaseExceptions. createException (Unknown Source)

At com. microsoft. jdbc. base. BaseExceptions. getException (Unknown Source)

At com. microsoft. jdbc. base. BaseData. getInteger (Unknown Source)

At com. microsoft. jdbc. base. BaseResultSet. getInt (Unknown Source)

At com. microsoft. jdbc. base. BaseResultSet. getInt (Unknown Source)

At edu. ccniit. jspCourse. db. prim. GetBookByID. main (GetBookByID. java: 39)

6.2 error description

Error message: 39 lines of code (int book_name = rs. getInt ("book_name");) error. The cause of the error is: in the book table, the Data Type of the book_name column is varchar (40), that is, String type. In the code, the String type in the database must be converted to int type, however, in Java, the String type cannot be automatically converted to the int type, so an error occurs.

Remember: the type of a field in the table. The same type should be used when we use the get ××× () method to obtain the data of this column. Otherwise, an error will be reported.

 

6.3 error Solution

Set

Int book_name = rs. getInt ("book_name"); Code Change

String book_name = rs. getString ("book_name ");

 

 


When running cmd in Java programming, the system prompts that the error is not an internal or external command, or a program or batch file that can be run.

Keyword error. pulic error. Change it to public class Test. Note that this test must be the same as the file name.
Class Name big Write Start

JDBC programming problems, always prompt missing (

Public void PreparedStatement getStatement () // remove void
{
Return stmt;
}
Public void Connection getConnection () // remove void

{
Return con;
}

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.