Devika Sujith
Devika Sujith

Reputation: 121

"Column Count doesn't match value count at row 1" E

I've encountered an error and I'm not able to figure out my mistake. I've done my research and haven't found an appropriate answer for my question.

This is my code:

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         

    String CN, CNo, MN, NT, SNo, VIP, T, D;
    CN = TF1.getText();
    CNo = TF2.getText();
    MN = TF3.getText();
    NT = TF4.getText();
    SNo = TF5.getText();
    VIP = TF6.getText();
    T = TF7.getText();
    D = TF8.getText();

    try
    {
        Class.forName("java.sql.DriverManager");
        Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/devika", "root", "rockgirl12");
        Statement stmt = (Statement) con.createStatement();
        String query = "INSERT INTO Maintenance VALUES ('"+CN+"',"+CNo+",'"+MN+"',"+NT+",'"+SNo+"','"+VIP+"','"+T+"','"+D+"');";
        stmt.executeUpdate(query);
        JOptionPane.showMessageDialog(this, "Record added succesfully!");
    }
    catch(Exception e)
    {
         JOptionPane.showMessageDialog(this, e.getMessage());
    }   
}                                        

What I'm trying to do here is I'm adding data to my SQL database through a form I designed in Java Netbeans. I've attached the form I've created here. My Form

Help would be greatly appreciated :)

Upvotes: 1

Views: 84

Answers (1)

e4c5
e4c5

Reputation: 53774

Exactly what the error says. The number of columns and the fields in valules do not match. This sort of insert without specifing the column names isn't the best practice by any stretch. You should do

String query = "INSERT INTO Maintenance(col1, col2, col3, col4,..) VALUES ('"+CN+"',"+CNo+",'"+MN+"',"+NT+",'"+SNo+"','"+VIP+"','"+T+"','"+D+"');";

In fact, you shouldn't be doing this sort of string concatenation either. It's far better to use prepared statements. The current approach does not ensure that the data is properly escaped before being saved.

Upvotes: 1

Related Questions