到官方网站下个SQL Server 2000 Driver for JDBC Service Pack 3及SQL Server 2000 Driver for JDBC 
在安装驱动时,在lib 目录下有三个jar文件夹,放在classpath路径下
package com.fuyou;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;public class DBConnection { private String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; private String user = "sa"; private String password = "fuyou";

private String url = "jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=pubs"; private Connection con = null;
    private Statement stmt = null;
public DBConnection() { try {
Class.forName(Driver); con = DriverManager.getConnection(url, user, password); } catch (ClassNotFoundException e) {
e.printStackTrace();
System.out.println("沒有找到驱动类!");
} catch (SQLException e) {
e.printStackTrace();
}
} public Connection getConnection() {
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) { e.printStackTrace();
}
return con;
} public Statement getStatement() { // 产生结果集可以滚动,可以更新,但不敏感

if (this.con == null) {
this.con = getConnection();
}
try {
stmt = this.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (SQLException e) { e.printStackTrace();
}
return stmt;
} public Statement getPreparedStatement(String sql) { // 预编译结果集可以滚动,可以更新,但不敏感

if (this.con == null) {
this.con = getConnection();
}
try {
stmt = this.con.prepareStatement(sql,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
} catch (SQLException e) { e.printStackTrace();
}
return stmt;
} public ResultSet executeQuery(String sql) { // 查询方法

ResultSet rs = null;
if (this.con == null) {
this.con = getConnection(); }
stmt = getStatement();

try {
rs = stmt.executeQuery(sql);
} catch (SQLException e) { e.printStackTrace();
}
return rs;
} public int executeUpdate(String sql) { // 更新方法,返回更新的记录数
Statement stmt = null;
int num = 0;
if (this.con == null) {
this.con = getConnection();
stmt = getStatement();
}
try {
this.con.setAutoCommit(false);
num = stmt.executeUpdate(sql);
con.commit();
con.setAutoCommit(true);
} catch (SQLException e) { e.printStackTrace();
try {
con.rollback();
} catch (SQLException e1) { e1.printStackTrace();
}
}
return num;
} public void closeResultset(ResultSet rs) { // 关闭结果集
if (rs != null) {
try {
rs.close();
} catch (SQLException e) { e.printStackTrace();
}
}
} public void closeStatement() {// 关闭stmt
if (this.stmt != null) {
try {
this.stmt.close();
} catch (SQLException e) { e.printStackTrace();
}
}
} public void closeConection() { // 关闭con
if (this.con != null) {
try {
this.con.close();
} catch (SQLException e) { e.printStackTrace();
}
}
}


}

解决方案 »

  1.   

    package com.fuyou;import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.sql.ResultSet;public class DBConnection {    private String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";    private String user = "sa";    private String password = "fuyou";
        
        private String url = "jdbc:microsoft:sqlserver://localhost:1433;DataBaseName=pubs";    private Connection con = null;
        private Statement stmt = null;
        public DBConnection() {        try {
                Class.forName(Driver);            con = DriverManager.getConnection(url, user, password);        } catch (ClassNotFoundException e) {
                e.printStackTrace();
                System.out.println("沒有找到驱动类!");
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }    public Connection getConnection() {
            try {
                con = DriverManager.getConnection(url, user, password);
            } catch (SQLException e) {            e.printStackTrace();
            }
            return con;
        }    public Statement getStatement() { // 产生结果集可以滚动,可以更新,但不敏感
            
            if (this.con == null) {
                this.con = getConnection();
            }
            try {
                stmt = this.con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_UPDATABLE);
            } catch (SQLException e) {            e.printStackTrace();
            }
            return stmt;
        }    public Statement getPreparedStatement(String sql) { // 预编译结果集可以滚动,可以更新,但不敏感
            
            if (this.con == null) {
                this.con = getConnection();
            }
            try {
                stmt = this.con.prepareStatement(sql,
                        ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_UPDATABLE);
            } catch (SQLException e) {            e.printStackTrace();
            }
            return stmt;
        }    public ResultSet executeQuery(String sql) { // 查询方法
            
            ResultSet rs = null;
            if (this.con == null) {
                this.con = getConnection();        }
            stmt = getStatement();
            
            try {
                rs = stmt.executeQuery(sql);
            } catch (SQLException e) {            e.printStackTrace();
            }
            return rs;
        }    public int executeUpdate(String sql) { // 更新方法,返回更新的记录数
            Statement stmt = null;
            int num = 0;
            if (this.con == null) {
                this.con = getConnection();
                stmt = getStatement();
            }
            try {
                this.con.setAutoCommit(false);
                num = stmt.executeUpdate(sql);
                con.commit();
                con.setAutoCommit(true);
            } catch (SQLException e) {            e.printStackTrace();
                try {
                    con.rollback();
                } catch (SQLException e1) {                e1.printStackTrace();
                }
            }
            return num;
        }    public void closeResultset(ResultSet rs) { // 关闭结果集
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {                e.printStackTrace();
                }
            }
        }    public void closeStatement() {// 关闭stmt
            if (this.stmt != null) {
                try {
                    this.stmt.close();
                } catch (SQLException e) {                e.printStackTrace();
                }
            }
        }    public void closeConection() { // 关闭con
            if (this.con != null) {
                try {
                    this.con.close();
                } catch (SQLException e) {                e.printStackTrace();
                }
            }
        }    
        
        
    }
      

  2.   

    一楼的真是太详细了。不过我们一般用jtds的驱动要好一些!