mssql2000连接数据库 mssql2005连接数据库 mssql2008连接数据库 之间的区别

// 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();
        }
    }
   
}
 

你可能感兴趣的:(sql,jdbc,Microsoft)