通过Netbeans向数据库添加值

时间:2012-04-22 20:35:45

标签: java database add

我收到SQLexception错误Coloumn Count与第1行的值计数不匹配。 我已经尝试改变数据库,因为我们根据语句检查了这些语句,但未能认识到这个问题。 Anny建议请...

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



}

}
}
});

1 个答案:

答案 0 :(得分:1)

看起来你错过了一些逗号。尝试查看PreparedStatement类来创建语句。使用它比尝试构建查询字符串要容易得多,它可以保护您免受sql injection的攻击。祝你好运!