JDBC zawsze testuje ostatni wiersz tabeli MySQL?
Mam klasę Manager, która zapisuje dane w tabeli SQL, a także pobiera wyniki z tabeli SQL i testuje te dane. Gdy uruchomię program, wyświetlona zostanie jedna ramka, która otrzyma identyfikator i hasło, a jeśli są poprawne, druga ramka pokaż.BUT Nie wiem, dlaczego po prostu testuje ostatni wiersz tabeli SQL? mam na myśli, że jeśli ustawię te pola tekstowe z innymi identyfikatorami i hasłami, z wyjątkiem ostatniego wiersza, to pokaże, że dane są błędne (wcześniej ustawiłem dla błędnych danych)
Klasa menedżera:
public static boolean Test(String userName, String password) {
boolean bool = false;
Statement stmt = null;
try {
stmt = conn.createStatement();
ResultSet rst = null;
rst = stmt.executeQuery("SELECT yahooId , password FROM clienttable");
while (rst.next()) {
if (rst.getString(1).equalsIgnoreCase(userName) && rst.getString(2).equalsIgnoreCase(password)) {
bool = true;
} else {
bool = false;
}
}
} catch (SQLException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
return bool;
}
wykonuję akcję mojego przycisku w ramce, która pobiera identyfikator i hasło oraz testuje je:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
submit();
} catch (ConnectException ex) {
JOptionPane.showMessageDialog(this, "You coudn't connect to the server successfully,try it again", "Sign_In Problem", JOptionPane.OK_OPTION);
}
clear();
}
private void submit() throws ConnectException {
String id = idField.getText();
char[] pass1 = passField.getPassword();
String pass = new String(pass1);
if (id.equals("") || pass.equals("")) {
Toolkit.getDefaultToolkit().beep();
JOptionPane.showMessageDialog(this, "You should enter an ID and password", "Sign_In Problem", JOptionPane.OK_OPTION);
return;
} else {
boolean b = Manager.Test(id, pass);
client.setCurrentName(id);
if (b == true) {
this.setVisible(false);
ListFrame frame = new ListFrame(client);
frame.setVisible(true);
} else {
JOptionPane.showMessageDialog(this, "You have entered wrong datas,try it again", "Sign_In Problem", JOptionPane.OK_OPTION);
return;
}
}
}
EDYTOWAĆ:
Zmodyfikowałem moją klasę menedżera (metodę testową), ale nadal działa jak przeszłość !!
public static boolean Test(String userName, String password) {
boolean bool = false;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
stmt = conn.prepareStatement("SELECT id FROM clienttable WHERE yahooId = ? AND password = ?");
stmt.setString(1, userName);
stmt.setString(2, password);
resultSet = stmt.executeQuery();
bool = resultSet.next();
} catch (SQLException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
resultSet.close();
stmt.close();
conn.close();
} catch (SQLException ex) {
Logger.getLogger(Manager.class.getName()).log(Level.SEVERE, null, ex);
}
}
return bool;
}