Update MySQL: java.sql.SQLException: No value spec

2019-09-18 23:10发布

问题:

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

回答1:

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