At least one parameter to the current statement is uninitialized ERROR

Viewed 41

I am new to Java and derby and a little lost.

I am using jframe and derby. Trying to do an email / password validation. At first I was concatenating with statement but read online it is best to use prepared statements. While using email as primary key, with statement I was getting a Lexical error because of the '@'. Now , with prepared statement I am getting a 'At least one parameter to the current statement is uninitialized'.

Not sure if I am entering something wrong....

This is the method I created.

public void login() {

   if ((jtxtEmail.getText().equals(""))) {

       JOptionPane.showMessageDialog(null, "Debes ingresar tu email y contraseña.", "Error al ingresar Usuario y contraseña", JOptionPane.ERROR_MESSAGE);

   } else {

       try {

           conectar();

           ps = conn.prepareStatement("select email,password,nombre from Usuarios where email=?");

           ResultSet rs = ps.executeQuery();

           while (rs.next()) {

               if (rs.getString(1).equals(jtxtEmail.getText()) && rs.getString(2).equals(jtxtPassword.getText())){

                   JOptionPane.showMessageDialog(null, "Bienvenido"+rs.getString(3), "Validacion Correcta", JOptionPane.INFORMATION_MESSAGE);

                   new DirectoryMale().setVisible(true);

               }else{

                   JOptionPane.showMessageDialog(null, "Debes ingresar tu email y contraseña.", "Error al ingresar Usuario y contraseña", JOptionPane.ERROR_MESSAGE);

               }
           }

           desconectar();

           ps.close();

       } catch (HeadlessException | SQLException ex) {

           JOptionPane.showMessageDialog(null, "Error.\n" + ex.getMessage());

       }
   }

}

Thanks a lot for helping a noob, lol...

1 Answers

You need to set a value for each parameter indicated by a ? before calling ps.executeQuery(). For example you could do:

ps = conn.prepareStatement(
  "select email,password,nombre from Usuarios where email=?");

ps.setString(1, "henry.fonda@mgm.com"); // added this line

ResultSet rs = ps.executeQuery();

Your query has declared only one parameter, and that's why there's a single setter on ps.

Related