0

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)
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Noob
  • 9
  • 1
  • 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) – Noob May 04 '18 at 04:24
  • this really shouldn't have been upvoted in the first place. Code dumps with no explanation of your goal and without formatting are poor quality. – Matthew Ciaramitaro May 04 '18 at 04:39
  • in case if the first column `int` is of `auto_increment`, then you need not include in `insert` statement. remove it from the column names list in the statement and then execute it. – Ravinder Reddy May 04 '18 at 04:40
  • Apart from having specified 6 columns and only 5 values, your code is highly unsafe as it is vulnerable to SQL injection. You really should not concatenate values into a query string. Use prepared statements with parameters. – Mark Rotteveel May 04 '18 at 07:48

3 Answers3

3

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

Mạnh Quyết Nguyễn
  • 17,677
  • 1
  • 23
  • 51
0
   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()+"')";
xrcwrn
  • 5,339
  • 17
  • 68
  • 129
0

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...

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51