Ottieni il nome utente e il tipo utente nel database del programma Java
-
23-12-2019 - |
Domanda
Ho fatto un programma che ha un database .. Ecco il mio database guardò ora:
Ho già potuto recuperare il nome utente con il tipo utente quando l'utente effettua il login con il loro nome utente.Ma ora ho un problema, il programma funziona correttamente, l'unica cosa è il programma mostra tutto il nome utente e il tipo utente anche se login con nome utente: Fuhans.
Quello che voglio è il programma mostra solo il nome utente registrato (ad esempio: Accesso con nome utente: Fuhans e il programma mostra la casella dei messaggi in cui visualizza il nome utente (Fuhans) e il tipo utente di quel nome utente (amministratore),Non tutto il nome utente nel database.
Come posso risolvere questo?
Ecco il mio codice: (scritto usando Java)
private void GetUsername(Connection conn, Statement state, ResultSet result)
{
try
{
String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
Class.forName(driver);
String url = "jdbc:odbc:Database";
conn = DriverManager.getConnection(url);
state = conn.createStatement();
String query = "select Username, UserType from Member";
result = state.executeQuery(query);
while(result.next())
{
user = result.getString("Username");
userType = result.getString("UserType");
_userInformation.setUser(user);
_userInformation.setUserType(userType);
_sound.PlaySound(1);
_infoBox.ShowMessageBox("Welcome: " + _userInformation.getUser() + " - " + _userInformation.getUserType() + " ! ", "Welcome" , 1);
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
_sound.PlaySound(2);
_infoBox.ShowMessageBox(e.getMessage(), "Error", 2);
_reminder = new Reminder(1);
JOptionPane.showOptionDialog(null, "Program will be closed due to error!", "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, new Object[]{}, null);
System.exit(0);
}
}
.
Molto grande grazie!
Soluzione
Modifica questa query:
select Username, UserType from Member
.
a
select Username, UserType from Member where Username=? and UserType=?
.
e passano il parametro corrispondente a ?
.Fondamentalmente stai ricevendo tutti i record senza filtraggio.
Altri suggerimenti
Hai bisogno di una clausola dove per raggiungere il membro registrato.