'Column count doesn't match value count at row 1 java [duplicate]
String query = "INSERT INTOusers(int,fname,lname,age,sname,bname) VALUES ('"+jTextField_FirstName.getText()+"','"+jTextField_LastName.getText()+"',"+jTextField_Age.getText()+",'"+jTextField_SName.getText()+"','"+jTextField_BName.getText()+"')";
Here is the stacktrace:
java.sql.SQLException: Column count doesn't match value count at row 1 at
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:965) at
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976) at
com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912) at
com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530) at
com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683) at
com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2482) at
com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1552)
Solution 1:[1]
You want to set 6 columns int, fname, lname, age, sname, bname
But you're providing 5 column values only.
Add one more column value, then it's OK
Solution 2:[2]
String query = "INSERT INTO users(int, fname, lname, age, sname, bname) VALUES
('"+jTextField_FirstName.getText()+"','"+jTextField_LastName.getText()+"',"
+jTextField_Age.getText()+",'"+jTextField_SName.getText()+"','"
+jTextField_BName.getText()+"')";
According to query you are inserting value for fname,lname,age,sname,bname, not for int your query shoud like below to set first value for int
String query = "INSERT INTO users(int, fname, lname, age, sname, bname) VALUES
('"+value_for_int+"','"+jTextField_FirstName.getText()+"','"+jTextField_LastName.getText()+"',"
+jTextField_Age.getText()+",'"+jTextField_SName.getText()+"','"
+jTextField_BName.getText()+"')";
Solution 3:[3]
You are providing six columns as follows:
INSERT INTO users (int, fname, lname, age, sname, bname)
But has provided only five values as follows:
jTextField_FirstName.getText()
jTextField_LastName.getText()
jTextField_Age.getText()
jTextField_SName.getText()
jTextField_BName.getText()
So provide one more value to int specified in the insert query and then you get no errors...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | xrcwrn |
| Solution 3 |
