微软对于jdbc的实现目前支持jdbc2,以前即SQL 2000的时候还只有JDBC1.0,要导入msbase.jar、mssqlserver.jar、msutil.jar这单个jar包,对应的连接代码为
Class.for("com.microsoft.jdbc.sqlserver.SQLServerDriver");
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=数据库名称",用户名,密码)。在SQL 2005时已经支持jdbc2.0,即只要导入sqljdbc.jar这一个jar包即可,连接代码变为
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
DriverManager.getConnection("jdbc:sqlserver://127.0.0.1:1433;databaseName=数据库名称;","用户名","密码");
上面都是在安装数据库是选择默认配置的情况下(端口1433)。jdbc2.0的实现应该具有兼容性即任然可以用在sql 2000上,只要对应的jar包和对应的连接代码相匹配应该就可以
具体案例如下:
// Java Document
package com.job36.test;
import java.sql.*;
public class connsql{
//1、连接SqlServer2000
//private String sDBDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//private String url="jdbc:microsoft:sqlserver://localhost:1433;......";
//2、连接SqlServer2005
//driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
//url=jdbc:sqlserver://localhost:1433;......
//3、连接SqlServer2008
//driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
//url=jdbc:sqlserver://192.168.3.29:1433;......
//以下为2008连接,事实上和2005连接一样的。都是用的jdbc1.2来完成的。
private String sDBDriver="com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String url="jdbc:sqlserver://192.168.3.29:1433;DatabaseName=crm";//Notice:DatabaseName
private String user="sa";
private String password="xxxx";//Notice: user and password.
Connection conn;
Statement stmt;
ResultSet rs;
public connsql()
{
try{
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e){
System.err.println("connsql():"+e.getMessage());
}
}
public void executeInsert(String sql){
try{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(SQLException ex){
System.err.println("connsql.executeUpdate:"+ex.getMessage());
}
}
public ResultSet executeQuery(String sql)
{
try
{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}
catch(SQLException ex)
{
System.err.println("connsql.executeQuery:"+ex.getMessage());
}
return rs;
}
/* public ResultSet executeQuery(String sql)
{
try{
conn=DriverManager.getConnection (url,user,password);
stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs=stmt.executeQuery(sql);
stmt.close();////////////////////////////I changed;
conn.close();////////////////////////////..........
}catch(SQLException ex){
System.err.println("connsql.executeQuery:"+ex.getMessage());
}
return rs;
}
*/ public void executeUpdate(String sql){
try{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(SQLException ex){
System.err.println("connsql.executeQuery:"+ex.getMessage());
}
}
public void executeDelete(String sql){
try{
conn=DriverManager.getConnection(url,user,password);
stmt=conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(SQLException ex){
System.err.println("connsql.executeDelete:"+ex.getMessage());
}
}
public void closeStmt(){
try{
stmt.close();
}
catch(SQLException e){
e.printStackTrace();
}
}
public void closeConn(){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
connsql conn = new connsql();
ResultSet rs = conn.executeQuery("select top 10 * from muser");
while(rs.next())
System.out.println(rs.getString(1));
rs.close();
conn.closeStmt();
conn.closeConn();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
jar包不同,url不同
相关链接代码也是有一定区别的
这个在你下的驱动包里有具体的实例
你可以看下