jsp 连接远程sql2005数据库

时间:2021-08-01 20:03:22

1. 下载Microsoft SQL Server 2005 JDBC Driver压缩包,解压缩后从中拷贝sqljdbc.jar放到项目的WEB-INF/lib中。

2. 部分代码:

     //加载JDBC驱动,注意红色部分与sql2000的区别     String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";      //连接服务器和数据库test,注意红色部分与sql2000的区别    String dbURL = "jdbc:sqlserver://数据库服务器的ip地址:1433; DatabaseName=数据库名";       //默认用户名    String userName = "sa";      //密码     String userPwd = "sa";       Connection dbConn=null;     PreparedStatement pstmt=null;     Statement stmt = null;     ResultSet rs = null;        try {      Class.forName(driverName);      dbConn = DriverManager.getConnection(dbURL, userName, userPwd);      stmt = dbConn.createStatement();     rs = stmt.executeQuery("Select * from ATable");     } catch (Exception e) {      e.printStackTrace();     }

3. 附上连sql2000的代码:

     //加载JDBC驱动     String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";      //连接服务器和数据库test,注意红色部分与sql2000的区别    String dbURL = "jdbc:microsoft://数据库服务器的ip地址:1433; DatabaseName=数据库名";       //默认用户名    String userName = "sa";      //密码     String userPwd = "sa";       Connection dbConn=null;     PreparedStatement pstmt=null;     Statement stmt = null;     ResultSet rs = null;        try {      Class.forName(driverName);      dbConn = DriverManager.getConnection(dbURL, userName, userPwd);      stmt = dbConn.createStatement();     rs = stmt.executeQuery("Select * from ATable");     } catch (Exception e) {      e.printStackTrace();     }