【问题标题】:Adding Values to database through Netbeans通过 Netbeans 向数据库添加值
【发布时间】:2012-04-22 20:35:44
【问题描述】:

我收到一个 SQLexception 错误,列计数与第 1 行的值计数不匹配。 我已尝试更改数据库以相应地检查语句,但未能识别问题。有什么建议请...

JButton btnNewButton_1 = new JButton("Save");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {


String PHN = textField_a.getText();
String Week1 = textField_4.getText();
String Week2 = textField_5.getText();
String Week3 = textField_6.getText();
String Week4 = textField_7.getText();

if (PHN.equals("") || (Week1.equals("") || (Week2.equals("") || (Week3.equals("") || (Week4.equals("") ))))) {
JOptionPane.showMessageDialog(frmTemplate, "Please enter values in all the fields.");   
}

else {

try {
db.stmt.executeUpdate("INSERT INTO weight (PHN, Week1, Week2, Week3, Week4)"+"VALUES"+"("+"'"+PHN+"',"+"'"+Week1+"',"+"'"+Week2+"'"+"'"+Week3+"'"+"'"+Week4+"')");
JOptionPane.showMessageDialog(frmTemplate, "New Record Added");

textField_a.setText(null);
textField_4.setText(null);
textField_5.setText(null);
textField_6.setText(null);
textField_7.setText(null);

}
catch (SQLException e) {
JOptionPane.showMessageDialog(frmTemplate, "The value you have entered already exist");
e.printStackTrace();



}

}
}
});

【问题讨论】:

    标签: java database add


    【解决方案1】:

    您好像少了几个逗号。尝试查看 PreparedStatement 类来创建你的陈述。使用它比尝试构建查询字符串要容易得多,它可以保护您免受sql injection 的侵害。祝你好运!

    【讨论】:

    • 谢谢,是的,我确实错过了逗号。
    • 很高兴听到您发现了问题。如果答案对您来说已经足够了,请通过勾选我的答案的复选标记来“接受”它,以指示未来的正确答案。如果不是,请告诉我我还能做些什么来提供帮助:)
    猜你喜欢
    • 2018-08-20
    • 1970-01-01
    • 2013-09-25
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 2021-02-08
    • 1970-01-01
    • 2011-10-25
    相关资源
    最近更新 更多