JDBC连接MSSQL2000和2005

(来源: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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值