由於系統需求,最近在開發過程中將系統從原來的MySQL轉移到SQL 2000下,其中遇到了諸多問題,花費了我不少時間。現在把我的經驗拿出來告訴大家,好讓大家少走彎路,節約時間。
首先是SQL 2000數據庫的安裝問題,在此我主要講些關於SQL 2000的版本與操作系統的兼容性問題:SQL 2000總共有7個不同版本,適應不同等級用戶的需求。
我試了一下,在XP系統下只有“個人開發版”能正常安裝而不出現錯誤,所以大家在安裝時要注意,具體安裝時的配置參照相關說明就可以了。
下面說明如何連接到SQL 2000數據庫,首先當然是要下載JDBC驅動程序,最好去微軟官方網站下載,然後將下載到的三個JAR包放入你的WEB應用的WEB-INF/lib/下。接下來編寫程序進行測試:
/***********************************************
/*
/*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.<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...
出現上邊錯誤的主要原因是默認的數據庫服務器端口 1433沒有打開,無法直接連接。如果升級到sp3則這個問題可以結決,我們再來看看升級之後,程序運行的結果:
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... <