|
sql server 2000 與 jdk的融合問題! sql server 2000 是當(dāng)今網(wǎng)絡(luò)編程中使用的比較多的一個數(shù)據(jù)庫系統(tǒng),而java則是流行的網(wǎng)絡(luò)編程語言,在網(wǎng)絡(luò)編程中涉及的比較多也比較重要的就是數(shù)據(jù)庫問題,java自身提供了對各類主流數(shù)據(jù)庫系統(tǒng)的支持,通過提供java.sql 庫,提供了一個統(tǒng)一的接口,使得可以在java環(huán)境下不必對程序作大規(guī)模的修改,只要更改相應(yīng)的驅(qū)動程序,即可實現(xiàn)對各類數(shù)據(jù)庫的操作,從而提高軟件的生存周期和降低軟件的開發(fā)成本和維護費用,在這種情況下sql server 2000 和java 的融合問題就顯得比較重要了,本文簡要地闡述一下sql server 2000 和jdk的融合問題。 系統(tǒng)需求: 數(shù)據(jù)庫:sql server 2000 sp3 驅(qū)動程序:sql server driver for jdbc java版本:jdk1.2以上 1:我們首先使用jdbc-odbc橋來實現(xiàn)數(shù)據(jù)庫的連接,這個相對簡單 首先使用sql server 企業(yè)管理其建立一個數(shù)據(jù)庫test,并建立一個簡單的表 first_table 建立odbc數(shù)據(jù)源 ,各步驟采用默認操作即可 編寫一個簡單的測試程序,該程序主要實現(xiàn)數(shù)據(jù)庫的連接,以及一個簡單的sql操作 ,代碼如下: /*********************************************** 運行結(jié)果如下: driver is ok conection is ok 1001 1002 1003 1004 1005 1006 Press any key to continue... 順利通過測試 2,我們這次不通過odbc橋來操作數(shù)據(jù)庫,我們采用sql server driver 來實現(xiàn)對sqlserver數(shù)據(jù)庫的操作, 這將是我們這篇文章的重點,因為jdbc-odbc橋是一種常見的操作windows系統(tǒng)數(shù)據(jù)庫的常用方法,但它存在的缺點很多,所以現(xiàn)在很多開發(fā)者都側(cè)重于使用sqlserver driver來操作,在這里我們通過一步步的調(diào)試,來加深讀者對這種連接的理解 在通常的理解下,只要我們裝了sqlserver driver for jdbc 我們便可進行數(shù)據(jù)庫編程,事實則不然,首先我們看下邊的代碼: /*********************************************** 這段代碼跟上變得代碼是一樣的,差別在于驅(qū)動,還有url,這是在使用sqlserver driver for jdbc 中遇到的困惑 按道理講,上邊這段代碼應(yīng)該沒錯,可首先我們來看一下,如果sqlser服務(wù)器沒有升級到sp3(在使用jdbc時,如果系統(tǒng)是xp或者2003務(wù)必要把sqlserver 升級到sp3,往上到處都有下的),我們看看運行結(jié)果 driver is ok java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establis hing socket. at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source ) at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source) at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source) at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source) at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Sou rce) at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown S ource) at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source) at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source) at java.sql.DriverManager.getConnection(DriverManager.java:523) at java.sql.DriverManager.getConnection(DriverManager.java:171) at DbTest.init(DbTest.java:32) at DbTest.<init>(DbTest.java:25) at DbTest.main(DbTest.java:46) Press any key to continue... 出現(xiàn)上邊錯誤的主要原因是默認的數(shù)據(jù)庫服務(wù)器端口 1433沒有打開,無法直接連接 。 如果升級到sp3則這個問題可以結(jié)決,我們再來看看升級之后,程序運行的結(jié)果 driver is ok conection is ok java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]對 象名 ‘room‘ 無效。 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.TDSExecuteRequest.processReplyToken( Unknown Source) at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Sour ce) at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType (Unknown Source) at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source) at com.microsoft.jdbc.base.BaseStatement.postImplExecu te(Unknown Source) at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source) at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown So urce) at com.microsoft.jdbc.base.BaseStatement.executeQuery(Unknown Source) at DbTest.init(DbTest.java:35) at DbTest.<init>(DbTest.java:25) at DbTest.main(DbTest.java:46) Press any key to continue... 在這兒,用戶已經(jīng)登陸上去,但是卻不能訪問里邊的數(shù)據(jù)表,出現(xiàn)這個問題的原因在于sa用戶為系統(tǒng)用戶 它雖然能夠登陸數(shù)據(jù)庫,但是storeManager數(shù)據(jù)庫里邊卻沒有這個用戶的訪問權(quán)限,所以,我們現(xiàn)在為這個數(shù)據(jù)庫重新建立一個用戶 share ,建立過程如下:在storeManager數(shù)據(jù)庫中選重用戶 ---〉新建用戶 -- 〉名稱選擇(這一步中有兩個關(guān)鍵點 1:身份驗證選sql身份驗證,默認數(shù)據(jù)庫選StoreManager)-〉建立新教色share ,此時更改程序,將用戶登陸名和密碼修改一下,重新運行程序 driver is ok conection is ok 1001 1002 1003 1004 1005 1006 Press any key to continue... 這次順利通過測試 總結(jié):sqlserve 和jdbc 的融合問題,關(guān)鍵涉及到sp3補?。ǘ丝陂_放)還有用戶問題,解決這兩個問題之后,剩余的便是sqlserver 操作問題了,還有一點在遠程操作的時候,要把sqlserver 組設(shè)置一下,在安全性里邊亦將身份驗證更改為sqlserve 驗證即可 |
|
|