Need Help! I have asked a question here earlier - MySQL/Java error related to this one but not identical (not sure of the protocols at SO for such questions). I am working with JDBC, MySQL and encountering errors. First, the code
public User find(String login) {
System.out.println("Trying to find the user...." + login);
User user = this.jdbcTemplate.queryForObject(
"select * from xyz where user_name = ?",
new Object[]{login},
new RowMapper<User>() {
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(Long.valueOf(rs.getInt(1)));
user.setUserName(rs.getString(2));
user.setPassword(rs.getString(3));
return user;
}
});
System.out.println("Found user..." + user);
return user;
}
public void create(User user) {
this.jdbcTemplate.update("INSERT INTO xyz (user_name,user_password) VALUES (default, default, ?, ?)",
new Object[] {user.getUserName(),user.getPassword()});
I need to give default values to user_name, user_password (the 2 columns) but don't know how and where in the "create(User user) block.
Here's the error for the code above:
SEVERE: Servlet.service() for servlet appServlet threw exception
java.sql.SQLException: Column count doesn't match value count at row 1
I have tried it without 'default', 'default' and just (?, ?) but that gives me yet another error.
SEVERE: Servlet.service() for servlet appServlet threw exception
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your
SQL syntax; check the manual that corresponds to your MySQL server version for the
right syntax to use near '?,?)' at line 1
1 Answer 1
Your column list only has 2 columns, and you're trying to insert 4 values.
INSERT INTO xyz (user_name,user_password) VALUES (default, default, ?, ?)
Drop the default values if the column has a default and is not specified in the column list.
4 Comments
user_name and user_password? Make sure that your columns are properly escaped if you're using a reserved word user ( user_id int(11) NOT NULL AUTO_INCREMENT, user_name varchar(100) NOT NULL, user_password varchar(100) NOT NULL, PRIMARY KEY (user_id) ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=latin1;