用JDBC編程的運行時錯誤及其解決




JDBC編程的運行時錯誤及其解決 

源代碼: 

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

1.1 錯誤信息: 

1.2 錯誤描述 

1.3 錯誤解決方法 

2、用戶 'sa' 登錄失敗 

2.1 錯誤信息: 

2.2 錯誤描述 

2.3 錯誤解決方法 

3Invalid parameter binding(s) 

3.1 錯誤信息: 

3.2 錯誤描述 

3.3 錯誤解決方法 

4、對象名 'jspBook' 無效 

4.1 錯誤信息: 

4.2 錯誤描述 

4.3 錯誤解決方法 

5、列名 'bookId' 無效 

5.1 錯誤信息: 

5.2 錯誤描述 

5.3 錯誤解決方法 

6Value can not be converted to requested type 

6.1 錯誤信息: 

6.2 錯誤描述 

6.3 錯誤解決方法 

 

源代碼:

  1. package edu.ccniit.jspCourse.db.prim;  
  2.    
  3. import java.sql.DriverManager;  
  4. import java.sql.SQLException;  
  5.    
  6. /** 
  7.  * 獲得指定ID的圖書的信息 
  8.  * @author Administrator 
  9.  * 
  10.  */  
  11. public class GetBookByID  
  12. {  
  13.    
  14. /** 
  15.  * @param args 
  16.  * @throws ClassNotFoundException  
  17.  * @throws SQLException  
  18.  */  
  19. public static void main(String[] args) throws ClassNotFoundException, SQLException  
  20. {  
  21. Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");  
  22. //-----------------------------------------------------------------------------------------  
  23. String dbUrl = "jdbc:microsoft:sqlserver://localhost:1433";  
  24. java.sql.Connection con = DriverManager.getConnection(dbUrl,"sa","123");  
  25. //-----------------------------------------------------------------------------------------  
  26. String sqlStr = "select * from jspBook where bookId=?";  
  27. java.sql.PreparedStatement pst = con.prepareStatement(sqlStr);  
  28. //------------------------------------------------------------------------------------------  
  29. pst.setInt(33);  
  30. //------------------------------------------------------------------------------------------  
  31. java.sql.ResultSet rs = pst.executeQuery();  
  32. //------------------------------------------------------------------------------------------  
  33. while(rs.next())  
  34. {  
  35. String book_id = rs.getString("bookId");  
  36. String book_name = rs.getString("book_name");  
  37. System.out.println(book_id+"      "+book_name);  
  38. }  
  39. pst.close();  
  40. con.close();  
  41.    
  42. }  
  43.    
  44. }  
package edu.ccniit.jspCourse.db.prim;
 
import java.sql.DriverManager;
import java.sql.SQLException;
 
/**
 * 獲得指定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");
//-----------------------------------------------------------------------------------------
String sqlStr = "select * from jspBook where bookId=?";
java.sql.PreparedStatement pst = con.prepareStatement(sqlStr);
//------------------------------------------------------------------------------------------
pst.setInt(3, 3);
//------------------------------------------------------------------------------------------
java.sql.ResultSet rs = pst.executeQuery();
//------------------------------------------------------------------------------------------
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();
 
}
 
}

報錯如下:

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

1.1錯誤信息:

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 錯誤描述

    錯誤信息:SQLServerJDBC驅動程序類com.microsoft.jdbc.sqlserver.SQLServerDriver無法找到。

    造成錯誤的原因:沒有將SQLServer的3個驅動程序文件放到項目的構建路徑中。

 1.3錯誤解決方法

    將SQLServer的三個驅動文件放到項目的構建路徑中。操作步驟是:

    步驟1:在項目中新建一個名稱爲lib的目錄,操作順序爲:

            鼠標右擊項目名-〉新建-〉文件夾

步驟2:將數據庫驅動的3個文件拷貝到lib目錄下,操作順序爲

        拷貝數據庫取得的3個文件->選擇項目中的lib目錄並單擊鼠標右鍵->選擇粘貼

步驟3:鼠標右擊項目名-〉選擇構建路徑-〉配置構建路徑-〉選擇庫

標籤項,將打開以下頁面:

 

步驟4:點擊添加按鈕,並選擇jspCourseExample項目下的lib目錄中的3個文件,並點擊確定按鈕,界面如下:


2、用戶 'sa' 登錄失敗

2.1 錯誤信息:

Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]用戶 'sa' 登錄失敗。

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 錯誤描述

    錯誤信息:用sa用戶名和123密碼無法登錄SQLServer數據庫

2.3 錯誤解決方法

    請輸入正確的數據庫密碼,如果數據庫沒有密碼或密碼忘記,可以在

SQLServer的企業管理器中的安全中選項中來修改sa用戶密碼。

 

 

3Invalid 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 錯誤描述

    錯誤信息:對SQL語句中的?指定實際數據時出錯,一般是將?的序號指定錯了,SQL中的?的序號從1開始。並且指定的實際數據的值得類型必須與數據庫中對應的列的數據類型,並且要與pst.set×××(…)中×××指定的類型相一致。

3.3 錯誤解決方法

  將pst.setInt(3, 3);代碼改爲pst.setInt(3, 3);

 

 

 

4、對象名 'jspBook' 無效

4.1 錯誤信息:

Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對象名 'jspBook' 無效。

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.executeQueryInternal(Unknown Source)

at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)

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

4.2 錯誤描述

    錯誤信息:代碼的34"select * from jspBook where bookId=?"中,jspBook代表表名,即數據庫中並沒有名稱爲jspBook的表。

4.3 錯誤解決方法

步驟1:一定要確保數據庫的URL中一定要有databaseName參數,否則JDBC

連接的就是master庫,但master庫並不是我們要進行操作的數據庫。

  步驟2:指定好databaseName參數後,查看數據庫中是否有名稱爲jspBook的表。此處應  將jspBook改爲正確的表名,即將

String sqlStr = "select * from jspBook where bookId=?";代碼改

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

 

5、列名 'bookId' 無效

5.1 錯誤信息:

Exception in thread "main" java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]列名 'bookId' 無效。

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.executeQueryInternal(Unknown Source)

at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)

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

5.2 錯誤描述

    錯誤信息:34行代碼將用"select * from  book where bookId=?"查詢數據庫,其中bookId代表列名,但book表中並沒有名稱爲bookId的列。

5.3 錯誤解決方法

  將jspBook改爲正確的表名,即將

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

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

 

6Value can not be converted to requested type

6.1 錯誤信息:

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 錯誤描述

    錯誤信息:39行代碼(int book_name = rs.getInt("book_name");)出錯。出錯的原因是:在book表中,book_name列的數據類型是varchar(40),即 String類型,而代碼中要將數據庫中String類型轉換成int類型,但是在JavaString類型不能自動轉換成int類型,所以出錯。

    記住:表中某個字段是什麼類型,我們用get×××()方法獲得此列的數據時,也應該用同樣的類型,否則會報錯。

 

6.3 錯誤解決方法

  將

int book_name = rs.getInt("book_name");代碼改

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

發佈了19 篇原創文章 · 獲贊 16 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章