Когда создается ResultSet
, оно указывает на строку «перед первым» результата. Вам нужно попытаться переместить его в первый ряд (используя next()
), а затем сравнить его содержимое. Если такой строки нет, вы можете вернуть false
:
public boolean checking(String name, String id_number, String tableName){
if (conn==null) {
connect();
}
try{
Statement stmt = conn.createStatement();
// Side note: Depending on where the parameters come from, this may be vulnarable
// to an SQL Injection attack.
// Make sure you properly validate/sanitize the arguments
ResultSet rs = stmt.executeQuery("select * from " + tableName + " where name = " + "'"+name+"'");
// Check if there's even such a row:
if (!rs.next()) {
return false;
}
// Check the id number
return Id_number.equals(rs.getString(4));
} catch(Exception e){
e.printStackTrace(); // Or some proper handling...
}
return false;
}