Sage Hopkins
Sage Hopkins

Reputation: 173

MySQL error: Column count doesn't match value count at row 1

I am attempting to insert a new row into a MySQL database. However I am receiving

java.sql.SQLException: Column count doesn't match value count at row 1

each time I run the following piece of code.

JButton btnSubmit = new JButton("Submit");
    btnSubmit.addActionListener(new ActionListener() {
        Connection connection = dbConnector.dbConnector();
        public void actionPerformed(ActionEvent arg0) {
            try{
            String query = "insert into ChampionData(`Champion`,`User`,`TimeStamp`,`Opponet`,`Lane`,`Role`,`Kills`,`Deaths`,`Assists`,`CS`,`Gold`,`W/L`,`TotalGameTime`) values(?,?,?,?,?,?,?,?,?,?,?,?)";
            PreparedStatement pstInsert = connection.prepareStatement(query);
            pstInsert.setString(1, textField.getText());
            pstInsert.setString(2, textField_1.getText());
            pstInsert.setString(3, textField_2.getText());
            pstInsert.setString(4, textField_3.getText());
            pstInsert.setString(5, textField_4.getText());
            pstInsert.setString(6, textField_5.getText());
            pstInsert.setString(7, textField_6.getText());
            pstInsert.setString(8, textField_7.getText());
            pstInsert.setString(9, textField_8.getText());
            pstInsert.setString(10, textField_9.getText());
            pstInsert.setString(11, textField_10.getText());
            pstInsert.setString(12, textField_11.getText());
            pstInsert.execute();

            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });

Error received each time the code is executed.

java.sql.SQLException: Column count doesn't match value count at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:882)
at me.sage.hopkins.gui.and.mysql.Admin$2.actionPerformed(Admin.java:228)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

https://i.sstatic.net/WukHu.png

Upvotes: 1

Views: 441

Answers (1)

Felix Pamittan
Felix Pamittan

Reputation: 31879

The problem is with this line:

String query = "insert into ChampionData(`Champion`,`User`,`TimeStamp`,`Opponet`,`Lane`,`Role`,`Kills`,`Deaths`,`Assists`,`CS`,`Gold`,`W/L`,`TotalGameTime`) values(?,?,?,?,?,?,?,?,?,?,?,?)";

The table ChampionData has 13 columns:

`Champion`
`User`
`TimeStamp`
`Opponet`
`Lane`
`Role`
`Kills`
`Deaths`
`Assists`
`CS`
`Gold`
`W/L`
`TotalGameTime`

While the row you're trying to insert has only 12 columns (as evidenced by the 12 question marks)

Upvotes: 2

Related Questions