public class www { public static void main(String[]args) throws Exception { //第一步:加載驅動(驅動jar包必須加入classpath中) Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); //第二步:創建鏈接(根據實際狀況替換數據庫的主機地址、端口號、數據庫明、登陸名、密碼) Connection conn = DriverManager.getConnection("jdbc:sqlserver://localhost:1433;databaseName=shijiuban1", "sa", "111111"); System.out.println("當前鏈接到的數據庫="+conn.getCatalog());//查看當前鏈接到的數據庫名 //第三步:建立Statement對象 Statement stmt = conn.createStatement();//只讀的結果集 //Statement stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);//可更新的結果集 //第四步:執行操做(增刪改查) ResultSet rs = stmt.executeQuery("select * from stt"); //處理結果 while(rs.next()){ System.out.println(rs.getString("1")); } //第五步:關閉鏈接 conn.close(); } }