简体   繁体   中英

Update MySQL: java.sql.SQLException: No value specified for parameter 8

Not to sure what I am missing here but nothing seems to be working for me. I tried several approaches and none seemed to work. Any idea?

Here is my Code:

btnUpdate = new JButton("Update");
btnUpdate.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        String pid = textIDField.getText();
        String fn = textFNameField.getText();
        String ln = textLNameField.getText();
        String add = addressField.getText();
        String city = cityField.getText();
        String phone = phoneField.getText();
        String mid = medIDField.getText();

        try{
            Connection conn = DriverManager.getConnection("dbinfo");
            String query = "UPDATE Patient SET PatID=?,FirstName=?,LastName=?,Address=?,City=?,Phone=?,MedID=? WHERE PatID=? AND MedID=?";

            PreparedStatement pst = conn.prepareStatement(query);
            pst.setString(1, pid);
            pst.setString(2, fn );
            pst.setString(3, ln );
            pst.setString(4, add );
            pst.setString(5, city );
            pst.setString(6, phone );
            pst.setString(7, mid );

            pst.executeUpdate();
            JOptionPane.showMessageDialog(null, "Patient Updated");
            pst.close();

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

please you have not set the values for the last two parameter so check below

 try{
                Connection conn = DriverManager.getConnection("dbinfo");
                String query = "UPDATE Patient SET  
          PatID=?,FirstName=?,LastName=?,Address=?,City=?,Phone=?,MedID=? ` `WHERE PatID=? AND MedID=?";

                PreparedStatement pst = conn.prepareStatement(query);
                pst.setString(1, pid);
                pst.setString(2, fn );
                pst.setString(3, ln );
                pst.setString(4, add );
                pst.setString(5, city );
                pst.setString(6, phone );
                pst.setString(7, mid );
                 pst.setString(8, mid );//this last two
                pst.setString(9, pid);

                pst.executeUpdate();
                JOptionPane.showMessageDialog(null, "Patient Updated");
                pst.close();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM