user1308007
user1308007

Reputation: 11

Adding Values to database through Netbeans

I get an SQLexception error Coloumn Count does not match value count at row 1. I have tried altering the databse as checked the statements accordinly but fail to recognise the issue. Anny suggestions please...

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



}

}
}
});

Upvotes: 0

Views: 574

Answers (1)

kentcdodds
kentcdodds

Reputation: 29061

It looks like you're missing a few commas. Try looking into the PreparedStatement class to create you're statements. It's a lot easier to work with than trying to build strings of your query and it protects you from sql injection. Good luck!

Upvotes: 1

Related Questions