SQLServer2005的数据库链接的jar包可以到微软网站上去下载,驱动和链接字符串如下:
1: String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
2: String url = "jdbc:sqlserver://localhost:1433;databaseName=pubs";
SQLServer2005 Express版本的TCP/IP端口默认是关闭的,可以这样修改:
1.打开SQL Server Configuration Manager;
2.设置TCP/IP为启用;
3.双击TCP/IP,在弹出的对话框中讲IPALL中的TCP端口设置为1433,点击确定。
4.重启SQLServer的服务。
JDBC链接代码:
1: import java.sql.Connection;
2: import java.sql.DriverManager;
3: import java.sql.SQLException;
4:
5:
6: public class JDBCTest {
7:
8: public static void main(String[] args) {
9: String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
10: String url = "jdbc:sqlserver://localhost:1433;databaseName=pubs";
11: try {
12: Class.forName(driver);
13: Connection conn = DriverManager.getConnection(url,"sa","sa");
14: if(conn != null){
15: System.out.println("链接成功");
16: }
17: } catch (ClassNotFoundException e) {
18: e.printStackTrace();
19: } catch (SQLException e) {
20: e.printStackTrace();
21: }
22: }
23: }