JAVA连接SQL Server实例1

import java.sql.*;

public class db
{
    
public static void main(String [] args)
    {
        
try
        {
            
// 连接SQL Server 2005的实例代码
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); 
            String url
="jdbc:sqlserver://localhost:1433;DatabaseName=demo"
            
//pubs为你的数据库的 
            String user="sa"
            String password
="zxm10"
            Connection conn
= DriverManager.getConnection(url,user,password); 
            Statement stmt
=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
            String sql
="select * from T_User"
            ResultSet rs
=stmt.executeQuery(sql); 
            
while(rs.next()) 
            {
                System.out.println(
"the first field is : " + rs.getString(1));
                System.out.println(
"the second field is : " + rs.getString(2));
            }
            rs.close(); 
            stmt.close(); 
            conn.close();

            
/* 连接SQL Server 2000的实例代码
            Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance(); 
            String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs"; 
            //pubs为你的数据库的 
            String user="sa"; 
            String password="hxh"; 
            Connection conn= DriverManager.getConnection(url,user,password); 
            Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); 
            String sql="select * from jobs"; 
            ResultSet rs=stmt.executeQuery(sql); 
            while(rs.next()) 
            {
                System.out.println("the first field is : " + rs.getString(1) + "<br>");
                System.out.println("the second field is : " + rs.getString(2) + "<br>");
            }
            rs.close(); 
            stmt.close(); 
            conn.close(); 
*/
            System.out.println(
"asdfas");
        }
        
catch (Exception ex)
        {
            System.out.print(
"error. " + ex.getMessage());
        }
    }
}
posted @ 2009-03-09 13:07  angushine  阅读(673)  评论(0编辑  收藏  举报