1. public class test {  
  2.     public static void main(String[] args) {  
  3.         try {  
  4.             String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";     //加载JDBC驱动   
  5.             Class.forName(driverName).newInstance();  
  6.             String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test";//连接本机SQLServer服务器的master库,端口为默认的1433 
  7.             String userName = "sa";     //登录账号  
  8.             String userPwd = "123";     //密码   
  9.             Connection conn = DriverManager.getConnection(url, userName,userPwd);                
  10.             java.sql.Statement stmt =conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);  
  11.             String sql = "select * from table1"// 表  
  12.             String sql2="INSERT INTO table1 VALUES ('Gates', 'Bill')";  
  13.             stmt.execute(sql2);  
  14.             ResultSet rs = ((java.sql.Statement) stmt).executeQuery(sql);                
  15.             while (rs.next()) {  
  16.                 System.out.println(rs.getString(1)+"    "+rs.getString(2)); // 取得第一列和第二列的值  
  17.             }  
  18.             rs.close();  
  19.             stmt.close();  
  20.             conn.close();  
  21.         } catch (Exception e) {  
  22.             e.printStackTrace();   
  23.             System.out.println(e.getMessage());  
  24.         }  
  25.     }