注意点:
1、要添加jdbc库,方法见jsp连接sql2000。
2、sql 2000端口1433要开放,即使用命令netstat -an可以查看到1433端口,如看不到1433端口,则sql2000要安装sqlsp3补丁,可从microsoft官方网站下载,解压后点击setup.bat即可(切记:并非解压完即完事,一定还要点击setup.bat安装,本人由于这个原因调试很长时间。)
3、可参考如下程序(代码正确,我测试连接数据库时所用):
package shan;
import java.sql.*;
public class TestJdbc {
Connection con;
Statement sta;
ResultSet rs;
String driver;
String url;
public TestJdbc()
{
driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
url = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=test";
init();
}
public void init()
{
try{
System.out.println("dddddddddddddddd");
Class.forName(driver);
System.out.println("driver is ok");
con = DriverManager.getConnection(url,"sa","sa");
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[])
{
System.out.println("KKKKKKKKKKK");
new TestJdbc();
}
}