Reputation: 611
I Have Created Registration Jsp and Servlet.When I Insert the Values Using Form,Values not inserted as well as Some Exception come on Apache Log.Can any one Help me to Find actual Error.in my query or Connection????
Jul 27, 2012 9:00:30 PM com.asiahospital.db.DataPersistor registerUser
SEVERE: null
java.sql.SQLException: Column count doesn't match value count at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2618)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2568)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:842)
at com.mysql.jdbc.StatementImpl.execute(StatementImpl.java:681)
at com.example.db.DataPersistor.registerUser(DataPersistor.java:29)
at com.example.servlet.RegistrationServlet.doPost(RegistrationServlet.java:40)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at [...]
The offending method:
public void registerUser(User user) {
try {
Statement stmt = con.createStatement();
String query = "INSERT INTO user VALUES ('" + user.getUserName()+ "','"
+ user.getPassword() + "','" + user.getFirstName() + "','"
+ user.getLastName() + "','" + user.getEmail() + "','false')";
stmt.execute(query);
} catch (SQLException ex) {
Logger.getLogger(DataPersistor.class.getName()).log(Level.SEVERE, null, ex);
}
}
Upvotes: 1
Views: 358
Reputation: 5145
In your insert statement, the number of columns you have supplied is not the same as the number of values you specified.
e.g.
insert into Table 1 (A, B, C) values (100, 1001, 1002, 1004).
=> 3 columns != 4 Values.
Upvotes: 1
Reputation: 220877
It is very likely that your query looks something like this:
INSERT INTO my_table(A, B, C) VALUES ('a', 'b')
Or like this (where my_table still has three columns)
INSERT INTO my_table VALUES ('a', 'b')
Quite obviously, that won't work, as you'll have to provide as many values as columns. From your comments, I suspect it's the second case. You had a pre-existing SQL statement and someone has added / removed a column from your user
table, rendering this SQL statement invalid
Upvotes: 2