JDBC连接 sqlServer2000 例子


例子一:

import java.sql.*;

class TestDB{
    private final String Driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver"; 
     private final String url = "jdbc:microsoft:sqlserver://localhost:1433;selectMethod=cursor;DatabaseName=pubs"; 
    private final String password = ""; 
    private final String userName = "sa"; 
    private Connection conn=null;
    private Statement stmt=null;
    private  ResultSet rs=null;
    public Connection getConnection(){ 
    try{ 
    Class.forName(Driver); 
    conn = DriverManager.getConnection(url,userName,password); 
    }catch(Exception e){ 
    e.printStackTrace(); 
    } 
    try{
        Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
                                              ResultSet.CONCUR_UPDATABLE);
        String sql = "select * from authors";
       rs = stmt.executeQuery(sql);
       }catch(Exception e){
        e.printStackTrace(); 
       }
       try{
           while (rs.next()) {
                  System.out.println(rs.getString(1));
                  System.out.println(rs.getString(2));
           }
       }catch(Exception e){
        e.printStackTrace(); 
       }
    
    return conn; 
} 
    public static void main(String []args){
       
        TestDB tt=new TestDB();
        tt.getConnection();

 


    }
    
}



例子二:
package database;

import java.sql.*;

/**
 * @author Administrator
 * 
 */
public class DBAccess {
    private Connection m_conn;

    private Statement m_stmt;

    String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

    String url = "jdbc:microsoft:sqlserver://localhost:1433;databasename=sc";

    String uName = "sa";

    String uPwd = "sa";

    /**
     * 
     */
    public DBAccess() {
        this.setDriver(driver);
        this.setConnection(url, uName, uPwd);
    }

    public DBAccess(String driver, String url, String userName, String userPWD) {
        try {
            m_conn = DriverManager.getConnection(url, userName, userPWD);
            m_stmt = m_conn.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public boolean setDriver(String driver) {
        try {
            Class.forName(driver);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean setConnection(String url, String userName, String userPWD) {
        try {
            m_conn = DriverManager.getConnection(url, userName, userPWD);
            m_stmt = m_conn.createStatement();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    // 处理查询
    public ResultSet sendQuery(String sql) {
        try {
            ResultSet m_rs = m_stmt.executeQuery(sql);
            return m_rs;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    // 处理数据更新
    public int sendUpdate(String sql) {
        try {
            return m_stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    // 测试程序
    public static void main(String[] arg) {
        DBAccess db = new DBAccess();
        String sql = "select * from Student";
        ResultSet rs = db.sendQuery(sql);
        try {
            if (rs != null) {
                while (rs.next()) {
                    System.out.println(rs.getInt("Sno") + " "
                            + rs.getString("Sname"));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}



文章出处:http://www.diybl.com/course/3_program/java/javajs/2008520/117059.html

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