Java login/password verification

Solved
ilhem.s Posted messages 34 Status Member -  
tarik45 Posted messages 2 Status Member -
Hello,
I’m trying to set up an authentication page but an exception occurs during execution, here it is:

java.sql.SQLException: Operation not allowed after ResultSet closed

Here is also a part of the code related to the login button

try{

Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url,log ,password);
Statement S=con.createStatement();

ResultSet resultaltLogin=S.executeQuery("Select login from user;");
while(resultaltLogin.next())
{
String LoginRecup=resultaltLogin.getString("login");
if (login.equals(LoginRecup))
{

ResultSet res=S.executeQuery("Select mot_de_passe from user where login='LoginRecup';");
while(res.next())
{
String MPRecup=res.getString("mot_de_passe");
if(MPRecup.equals(MP))
{
verif=true;
Accueil A=new Accueil();
A.show(true);
dispose();
}
}
}
}
if(!verif)
{
JOptionPane.showMessageDialog(null, "Verifier Login / Mot de passe", "Message d'erreur:"", JOptionPane.ERROR_MESSAGE);
T1.setText("");
PF.setText("");
}
con.close();

} catch(Exception a){System.out.println(a);}

5 answers

  1. malaik5 Posted messages 259 Registration date   Status Member Last intervention   33
     
    Hello,
    just a small remark,
    from an optimization perspective you can avoid running the query twice (the first to retrieve the login and the second to retrieve the password) because you end up connecting to your database twice and executing two queries, so it can slow down your execution, especially if you notice the two queries point to the same table!! (these are just recommendations, it doesn’t prevent your code from being correct), and to do it you do a select * from user after you compare the user-password pairs to what the user enters.

    ResultSet resultaltLogin=S.executeQuery("Select * from user;");
    while(resultaltLogin.next())
    {
    String LoginRecup=resultaltLogin.getString("login");
    String MPRecup=resultaltLogin.getString("mot_de_passe ");
    if (login.equals(LoginRecup) && MPRecup.equals(MP))
    {
    ...... // your processing here
    }

    try to correct like this maybe that will give something
    6
    1. malaik5 Posted messages 259 Registration date   Status Member Last intervention   33
       
      for the exception I don’t know why at the moment
      0
    2. ilhem.s Posted messages 34 Status Member 1
       
      what is this one? It worked
      Really thanks you helped me so much
      0