private static void LoginAdmin(){
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection conn = null;
PreparedStatement ps = null;
try {
conn = DriverManager.getConnection("jdbc:sqlserver://localHost:1433;databaseName=MySchool","sa","sqlpass");
String id = "zl";
String pwd = "1234";
String sql = "select Count(*) as login from Admin where LoginId = ? and LoginPwd = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, id);
ps.setString(2, pwd);
int r = ps.executeQuery(); //這裡報不能從結果集轉換為int
System.out.println(r == 1 ? "登錄成功!" : "登錄失敗!");
} catch (SQLException e) {
e.printStackTrace();
}finally{
if(ps != null){
ps.close();
}
if(conn != null){
conn.close();
}
}
}
ResultSet rs=ps.executeQuery();
if(rs.next()){System.out.println("登陸成功")}else{System.out.println("登陸失敗");}
可以參考我的博客http://blog.csdn.net/ld0807/article/details/53976180或者http://www.banzg.com/archives/104.html