用JDBC編程的執行階段錯誤及其解決大全,jdbc編程錯誤大全

來源:互聯網
上載者:User

用JDBC編程的執行階段錯誤及其解決大全,jdbc編程錯誤大全
用JDBC編程的執行階段錯誤及其解決

用JDBC編程的執行階段錯誤及其解決 

原始碼: 

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

1.1 錯誤資訊: 

1.2 錯誤描述 

1.3 錯誤解決方案 

2、使用者 'sa' 登入失敗 

2.1 錯誤資訊: 

2.2 錯誤描述 

2.3 錯誤解決方案 

3、Invalid 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 錯誤解決方案 

6、Value can not be converted to requested type 

6.1 錯誤資訊: 

6.2 錯誤描述 

6.3 錯誤解決方案 

 

原始碼:

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(); } }

報錯如下:
1、java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver1.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 錯誤描述

    錯誤資訊:SQLServer的JDBC驅動程式類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使用者密碼。

 

 

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

    錯誤資訊:對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=?";

 

6、Value can not be converted to requested type6.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類型,但是在Java中String類型不能自動轉換成int類型,所以出錯。

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

 

6.3 錯誤解決方案

  將

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

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

 

 


Java編程運行cmd時提示錯誤 不是內部或外部命令,也不是可啟動並執行程式或批次檔

關鍵字錯了,pulic 錯了,改成public class Test 注意,這個test必須要跟檔案名稱一樣
類名大寫開頭
 
JDBC編程遇到的問題,總提示缺少(

public void PreparedStatement getStatement()//去掉void
{
return stmt;
}
public void Connection getConnection()//去掉void

{
return con;
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.