这部分代码的目的是获取mySQL数据库中保存的密码,并与输入的密码进行比较。
public static boolean isPasswordMatching(String user, String pass) throws Exception{
boolean areMatching = true;
try {
Class.forName(driver);
//the query I want to execute
String myQuery = "SELECT user_password FROM myData WHERE user_id = \"" + user + "\"";
java.sql.Connection connection = DriverManager.getConnection(url,username,password);
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(myQuery);// <<== Result Set (WHAT SEEMS TO BE THE PROBLEM)
String rightPassword = rs.getString("user_password");
System.out.println("successfully checked passwords");
//makes areMatching true if passwords match, else, it will be false
if (pass == rightPassword) {areMatching = true;} else {areMatching = false;};
//cleanup
connection.close();
return areMatching;
}catch(SQLException se) {System.out.println("SE encountered a problem while trying to connect : "+ se);
}catch(Exception e) {System.out.println("encountered a problem while trying to connect : "+e);};
//return statement (boolean)
return areMatching;
}
答案 0 :(得分:0)
在获取数据之前,您需要使 resultset
指向第一个元素:
...
String rightPassword = "";
if(rs.next()) {
rightPassword = rs.getString("user_password");
}
...