2.解壓縮
3.設定CLASSPATH指向sqljdbc4.jar
4.將auth\x86\sqljdbc_auth.dll移至C:\Windows\System32(32位元)C:\Windows\SysWOW64(64位元)
5.連接程式碼
String connectionUrl = "jdbc:sqlserver://ip:1433;" + "databaseName=master;";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(connectionUrl, "帳號", "密碼");
String SQL = "SELECT TOP 10 * FROM table";
stmt = con.createStatement();
rs = stmt.executeQuery(SQL);
while (rs.next()) {
System.out.println(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) try {
rs.close();
} catch (Exception e) {
}
if (stmt != null) try {
stmt.close();
} catch (Exception e) {
}
if (con != null) try {
con.close();
} catch (Exception e) {
}
}
沒有留言:
張貼留言