(来源:http://hi.baidu.com/jack1999ok/blog/item/d3b4c00300111a074afb5168.html )
一、下载安装
1、SqlServer 2005 Express Edition
下载:http://msdn.microsoft.com/vstudio/express/sql/download/
安装完数据库后设置ICP/IP协议启动,具体如下:
(1)打开SQL Server Configuration Manager
(2)转到SQL Server 2005 Network Configuration->Protocols for SQLEXPRESS
(3)将TCP/IP设置为Enabled(启用)
(4)双击TCP/IP项,转到IP Addresses页
(5)IP All中设置TCP Port为1433
(6)重新启动服务
2、SqlServer2005数据库JDBC驱动
下载:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=a737000d-68d0-4531-b65d-da0f2a735707&displaylang=en
安装或者解压,取得sqljdbc.jar文件,该文件即为JDBC驱动。将sqljdbc.jar放到classpath。(web application中放在WEB-INF/lib下)
二、Sql Server2000和2005的连接代码区别
1、连接SqlServer2000
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=cms";
2、连接SqlServer2005
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
URL = "jdbc:sqlserver://localhost:1433;DatabaseName=cms";
注意:
Sql Server 2005 TCP/IP 是关闭的,可能导致连接失败,可用 Sql Server Configuration Manager启动它。
Using the sqljdbc.jar class library, applications must first register the driver as follows:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
When the driver is loaded, you can establish a connection by using a connection URL and the getConnection method of the DriverManager class:
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName=AdventureWorks;user=MyUserName;password=*****;";
Connection con = DriverManager.getConnection(connectionUrl);
In the JDBC API 4.0, the DriverManager.getConnection method is enhanced to load JDBC drivers automatically. Therefore, applications do not need to call the Class.forName method to register or load the driver when using the sqljdbc4.jar class library.
When the getConnection method of the DriverManager class is called, an appropriate driver is located from the set of registered JDBC drivers. sqljdbc4.jar file includes "META-INF/services/java.sql.Driver" file, which contains the com.microsoft.sqlserver.jdbc.SQLServerDriver as a registered driver. The existing applications, which currently load the drivers by using the Class.forName method, will continue to work without modification.
Note: sqljdbc4.jar class library requires a Java Runtime Environment (JRE) of version 6.0 or later.