java.sql.SQLException: No value specified for parameter 2 error

175 Views Asked by At

When I run my project then I type the password and username in my project then I encounter the error code: no value specified for parameter 2, even though all parameters should be filled in.

String user = username.getText();
String pass = password.getText();

String option = options.getSelectedItem().toString();
                     

if( user.equals("awd") || pass.equals("awd") ) {
    JOptionPane.showMessageDialog(rootPane, "Enter Username and Password","Login Error",1);
    password.setText(null);
    username.setText(null);
 } else {
    try {
        con = Connector.getConnection();
        pst = con.prepareStatement("SELECT * FROM logins WHERE username=? and password=?");
        pst.setString(1, user);
        pst.setString(2, pass);
        rs = pst.executeQuery();
        
         
        if(rs.next()){  
            String s1 = rs.getString("options");
            String un = rs.getString("username");
            
            if(option.equalsIgnoreCase("Admin") && s1.equalsIgnoreCase("admin")){
                Main mn = new Main(un);
                mn.setVisible(true);
                setVisible(false);
            }
            if(option.equalsIgnoreCase("Employee") && s1.equalsIgnoreCase("employee")){
                OwnerFrame ow = new OwnerFrame(un);
                ow.setVisible(true);
                setVisible(false);
            }
            
            else{
                JOptionPane.showMessageDialog(rootPane, "Username or Password are not Match", "Login Error",1);
          
            }
1

There are 1 best solutions below

3
Elliott Frisch On BEST ANSWER

SQL Bind parameters are one of the few places where we count from 1. This

pst.setString(0, user);
pst.setString(1, pass);

should be

pst.setString(1, user);
pst.setString(2, pass);