2014-11-24 124 views
1

我正在嘗試向MySQL數據庫中插入新行。不過我正在收到MySQL錯誤:列數不匹配第1行的值計數

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

每次我運行下面這段代碼。

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

錯誤每次執行該代碼時接收。

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) 

http://i.imgur.com/8LaWCLE.png

+0

是否需要引用列名?這是正確的引用字符? – MadProgrammer 2014-11-24 00:42:19

+0

我相信如此。我現在添加一個數據庫看起來像是什麼的屏幕截圖,看看是否可以解決這個問題。 – 2014-11-24 00:43:53

回答

2

問題是這一行:

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

ChampionData有13列:

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

當你試圖插入該行已只有12列(如12個問號所示)

+0

解決了這個問題。 – 2014-11-24 00:48:36

+0

很高興能幫到你。如果您沒有任何問題,請接受答案。 – 2014-11-24 00:55:25

相關問題