Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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);
          
            }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading