JSP连接SQL SERVER问题总结
由于系统需求,最近在开发过程中将系统从原来的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..
补充:Web开发 , Jsp ,