方法一String users=username.getText();
String psw=String.valueOf(password.getPassword());
/**
* 连接数据库
*/
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager
.getConnection(
"jdbc:microsoft:sqlserver://192.168.16.48:1433;DatabaseName=prise",
"sa", "sa");
stm = con.createStatement();
String sql = "select * from users where username='"+users+"'" ;
rs = stm.executeQuery(sql);// 执行给定的sql语句,返回resultset对象
/**
* 判断
*/
int i=0;
while (rs.next()) {
i++;
if(psw.equals(rs.getString("password"))){
System.out.println("存在此用户,用户名和密码分别是:");
System.out.println(rs.getString("username") + "/t"+ rs.getString("password"));
break;
}
if(!psw.equals(rs.getString("password"))){
System.out.println("密码错误,请重新输入");
break;
}
}
if(i==0){
System.out.println("没有此用户");
}
方法二:
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); con = DriverManager .getConnection( "jdbc:microsoft:sqlserver://192.168.16.48:1433;DatabaseName=prise", "sa", "sa"); stm = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE); String sql="select * from users"; rs = stm.executeQuery(sql);// 执行给定的sql语句,返回resultset对象 /** * 判断 */ while (rs.next()) { if(user.equals(rs.getString("username"))&&psw.equals(rs.getString("password"))){ System.out.println("存在此用户,用户名和密码分别是:"); System.out.println(rs.getString("username") + "/t"+ rs.getString("password")); break; } if(user.equals(rs.getString("username"))&&!psw.equals(rs.getString("password"))){ System.out.println("密码错误,请重新输入"); break; } } if(rs.isAfterLast()){ System.out.println("没有此用户请注册"); }