【问题标题】:jTable update button and add newjTable 更新按钮并添加新的
【发布时间】:2015-01-13 15:31:33
【问题描述】:

下面是我的 jFrame btn_update 代码:

private void txt_updateActionPerformed(java.awt.event.ActionEvent evt) {                                           
    try{
        String Value1=txt_Emp_ID.getText();
        String Value2=txt_Name.getText();
        String Value3=txt_Surname.getText();
        String Value4=txt_age.getText();
        String Value5=txt_Username.getText();
        String Value6=txt_Password.getText();

        String sql = "update employee set Emp_ID=?, name=?,surname=?,age=?,username=?,password=?";           
        pst.setString(1, Value1);
        pst.setString(2, Value2);
        pst.setString(3, Value1);
        pst.setString(4, Value1);
        pst.setString(5, Value1);
        pst.setString(6, Value1);
        pst=conn.prepareStatement(sql);
        rs=pst.executeQuery();
        JOptionPane.showMessageDialog(null, "Updated!!");
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, e);
    }
}                                    

和我的配置:

Connection conn=null;
ResultSet rs = null;
PreparedStatement pst = null;

当我尝试按下更新按钮时,我得到了这个:

java.sql.SQLException:Parameter index out of Range (2>参数个数,即1) 有人可以帮我吗?

【问题讨论】:

  • 1) 你应该调用conn.prepareStatement(sql)设置它的参数。 2)您将Value1 设置为您准备好的语句中的所有参数,这可能不是您想要做的。

标签: java swing jdbc


【解决方案1】:

几个问题

  • 您的异常消息表明PreparedStatement 只有两个参数。在设置参数之前分配变量
  • 复制/粘贴错误,其中参数 3-6 都是 Value1 而不是 Value3Value4 等。
  • executeQuery 用于查询数据库。使用executeUpdate 进行数据库写入操作

结果:

preparesStatement = connection.prepareStatement(sql);
preparesStatement.setString(1, value1);
...// etc.
preparesStatement.setString(6, value6);

【讨论】:

  • 我这样做了,现在我收到以下错误:java.sql.SQLException: 无法使用 executeQuery() 发出数据操作语句。
  • 使用int rows = pst.executeUpdate();
猜你喜欢
  • 1970-01-01
  • 2015-07-27
  • 2016-02-28
  • 2011-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-10
  • 1970-01-01
相关资源
最近更新 更多