@Test
public void testConnDb() {
Connection con = null;// 创建一个数据库连接
PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
ResultSet result = null;// 创建一个结果集对象

String url = "jdbc:sqlserver://192.168.1.200:1433; DatabaseName=jianzhi";
String user = "sa";
String pass = "sa123456";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(url, user, pass);
String sql = "select top 20 * from t_student where studentName like ? order by studentId desc";
pre = con.prepareStatement(sql);
pre.setString(1, "王%");
result = pre.executeQuery();
while (result.next()) {
System.out.println("学号:" + result.getString("SID") +
" 姓名:" + result.getString("StudentName") +
" 性别:" + result.getString("sex"));
}
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
try {
if (result != null)
result.close();
if (pre != null)
pre.close();
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
System.out.println(e.getMessage());
}
}

}