【问题标题】:Update MySQL: java.sql.SQLException: No value specified for parameter 8更新 MySQL:java.sql.SQLException:没有为参数 8 指定值
【发布时间】:2016-08-29 03:06:15
【问题描述】:

不确定我在这里缺少什么,但似乎没有什么对我有用。我尝试了几种方法,但似乎都没有奏效。任何想法?

这是我的代码:

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

【问题讨论】:

  • 为什么不为参数 8 和 9(where 子句中的值)提供值?
  • SQL 中有多少个?9。你设置了多少参数? 7。错误消息“No value specified for parameter 8”的哪一部分不清楚?
  • 你期望它足够聪明,知道 pid 和 mid 被使​​用了两次。您需要两次提供它。真的,为什么要更新那些不会改变的值?
  • 谢谢你打我的头。我更正了参数和我的 id 的滥用。

标签: java mysql eclipse swing jdbc


【解决方案1】:

请您没有设置最后两个参数的值,所以请检查下面

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 2020-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多