/*********************************************** /* /*DBTest.java /* /******************************************* */ import Java.sql.*; public class DBTest { Connection con; Statement sta; ResultSet rs; String driver; String url; String user; String pwd; public DBTest() { driver = "com.microsoft.jdbc. sqlserver.SQLServerDriver";; url = "jdbc:microsoft:sqlserver: //localhost:1433;DatabaseName =test"; //test為數據庫名 user = "sa"; pwd = "sa"; //請更改為你相應的用戶和密碼 init(); } public void init() { try{ Class.forName(driver); System.out.println("driver is ok"); con = DriverManager. getConnection(url,user,pwd); System.out.println("conection is ok"); sta = con.createStatement(); rs = sta.executeQuery ("select * from room"); while(rs.next()) System.out.println (rs.getInt("roomNum")); }catch(Exception e) { e.printStackTrace(); } } public static void main(String args[]) //自己替換[] { new DBTest(); } }
按道理講,上邊這段代碼應該沒錯,可首先我們來看一下,如果sqlser服務器沒有升級到sp3(在使用jdbc時,如果系統是xp或者2003務必要把sqlserver 升級到sp3,往上到處都有下的),我們看看運行結果:
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. (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(DriverManager.java:523) at java.sql.DriverManager. getConnection(DriverManager.java:171) at DbTest.init(DbTest.Java:32) at DbTest. (DbTest.java:25) at DbTest.main(DbTest.Java:46) Press any key to continue...
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 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.postImplExecu te(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. BaseStatement.executeQuery(Unknown Source) at DbTest.init(DbTest.Java:35) at DbTest. (DbTest.java:25) at DbTest.main(DbTest.Java:46) Press any key to continue...
在這兒,用戶已經登陸上去,但是卻不能訪問裡邊的數據表,出現這個問題的原因在於sa用戶為系統用戶,它雖然能夠登陸數據庫,但是test數據庫裡邊卻沒有這個用戶的訪問權限,所以,我們現在為這個數據庫重新建立一個用戶share,建立過程如下:
driver is ok conection is ok 1001 1002 1003 1004 1005 1006 Press any key to continue...
這次順利通過測試 其實這些小問題花了我一個晚上才解決,真是浪費時間,所以寫下來希望能使遇到類似問題的朋友不要重蹈覆轍,在此提醒大家遇到問題時多裡問問,這樣你學到的會更多,更節省時間,更有效率。
總結:Sqlserve 和JDBC 的融合問題,關鍵涉及到sp3補丁(端口開放)還有用戶問題,解決這兩個問題之後,剩余的便是Sqlserver 操作問題了,還有一點在遠程操作的時候,要把Sqlserver 組設置一下,在安全性裡邊亦將身份驗證更改為Sqlserve 驗證即可。