0

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



}

}
}
});

1 Answer 1

1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, yes indeed I was missing commas.
Glad to hear you found the problem. If the answer was sufficient for you, please "accept" it by checking the check mark by my answer to indicate the correct answer for future comers. If it wasn't, let me know what more I can do to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.