0

I have created 2 JFrames: "Patients_Login" frame and "Doctors_Login" frame. I wrote an actionPerformed method in "Login" button in "Patients_Login" frame, and it works fine. Although i copy-pasted the exact same code to the "Login" button in "Doctors_Login" frame, but it gives me this error:

java.sql.sqlexception: no value specified for parameter 1

click here to see a capture of the error

This is the code from login button in "Patients_Login" frame (works):

    btnLogIn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try{



                //connecting to database
                Class.forName("com.mysql.jdbc.Driver");
                Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/clinic", "root", "newpass"); 

                //verifying ID and password code   
                String query="select * from patients where P_ID=? and Password=?";   
                PreparedStatement pst=con.prepareStatement(query);

                pst.setString(1, idTXT.getText());
                pst.setString(2, passwordField.getText());

                ResultSet rs=pst.executeQuery();
                int count =0;
                while(rs.next()){
                    count=count+1;
                }
                if (count ==1){

                    JOptionPane.showMessageDialog(null, "Welcome!");

                    patient_login.setVisible(false);
                    patient_frame.setVisible(true);
                    patient_appointments.setVisible(false);



                }

                else{
                    JOptionPane.showMessageDialog(null, "Incorrect username or password.");
                }

                rs.close();
                pst.close();

                //second query: displaying user info in JLabels 
                String query1="select * from patients where P_ID=?";
                PreparedStatement pst1=con.prepareStatement(query1);

                pst1.setString(1, idTXT.getText());
                ResultSet rs1=pst1.executeQuery();

                while (rs1.next()){

                    IDdraw_LBL.setText(rs1.getString("P_ID"));
                    namedraw_LBL.setText(rs1.getString("Name"));
                    msdraw_LBL.setText(rs1.getString("Medical_Situation"));

                }

                pst1.close();

            }catch(Exception exx) {
                exx.printStackTrace();
            }

        }
    });

And this is the code from login button in "Doctor_Login" frame (gives error):

    btnLogin.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            try{



                                //connecting to database
                                Class.forName("com.mysql.jdbc.Driver");
                                Connection con=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/clinic", "root", "newpass"); 

                                //verifying ID and password code   
                                String query="select * from doctors where ID=? and Password=?";   
                                PreparedStatement pst=con.prepareStatement(query);

                                pst.setString(1, docidTXT.getText());
                                pst.setString(2, passwordField.getText());

                                ResultSet rs=pst.executeQuery();
                                int count =0;
                                while(rs.next()){
                                    count=count+1;
                                }
                                if (count ==1){

                                    JOptionPane.showMessageDialog(null, "Welcome!");

                                    Doctor_login.setVisible(false);
                                    doctor_Appointments.setVisible(false);
                                    Doctor_Frame.setVisible(true);

                                }

                                else{
                                    JOptionPane.showMessageDialog(null, "Incorrect username or password.");
                                }

                                rs.close();
                                pst.close();

                                //second query: displaying user info in JLabels 
                                String query1="select * from doctors where ID=?";
                                PreparedStatement pst1=con.prepareStatement(query1);

                                pst1.setString(1, docidTXT.getText());
                                ResultSet rs1=pst1.executeQuery();

                                while (rs1.next()){

                                    DRnamedraw_LBL.setText(rs1.getString("Name"));
                                    no_of_patientsLBL.setText(rs1.getString("Patients"));


                                }

                                pst1.close();

                            }catch(Exception exx) {
                                exx.printStackTrace();
                            }

                        }
                    });

Obviously, both codes are exact same, but i changed the objects names and sql statements.

Note: the error occurs once I run the project, before I click on login button.

Note2: after I click "OK" on the error pop-up, the project works fine.

Any help or comment will be appreciated.

Maryamsi
  • 1
  • 2
  • 1
    Possible duplicate of [SQLException: No value specified for parameter 1](https://stackoverflow.com/questions/8182880/sqlexception-no-value-specified-for-parameter-1) – achAmháin May 01 '18 at 15:45
  • It doesn't solve my problem. But thanks anyway! – Maryamsi May 01 '18 at 18:02

0 Answers0