java连接SQLserver2008
package sqltest; import java.sql.*; class Dbtest { public static void main (String args[]) { String url=null; String query = "SELECT * FROM Employee"; Connection con=null; Statement stmt=null; ResultSet rs=null; try { url="jdbc:sqlserver://127.0.0.1:1433;databaseName=CheckAttendance;user=sa;password=123456"; //jdbc:sqlserver://127.0.0.1:1433;databaseName=test;user=sa;password=sa123 //jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=CheckAttendance Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con= DriverManager.getConnection(url,"sa","123456"); //***************************************************** stmt = con.createStatement (); // 提交一个请求,获得SQL语句的结果集,结果集为一个对象,被rs所引用。 rs = stmt.executeQuery (query); // 对结果集进行遍历,显示其内容。 while (rs.next()) { System.out.println("age:"+rs.getInt("eage")); } } catch (SQLException ex) { System.out.println ("发生SQL异常"); } catch (Exception ex) { ex.printStackTrace ();} finally{ try{ if (rs!=null) rs.close(); if (stmt!=null) stmt.close(); if (con!=null) con.close(); } catch (SQLException ex) {} } } }
参考http://www.cnblogs.com/striver-zhu/p/4158609.html

浙公网安备 33010602011771号