我试图在MySQL数据库中插入一个新行。但是我收到了
java.sql.SQLException:列数与第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)
答案 0 :(得分: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个问号证明)