0

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
asked Aug 19, 2013 at 19:54

1 Answer 1

0

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.

answered Aug 19, 2013 at 19:55
Sign up to request clarification or add additional context in comments.

4 Comments

Thx :) I just edited the question to indicate the errors when I try it without 'default' and just (?, ?).
The MySQL table does not have default values for these columns.
@user2480526 You've changed your question at that point. Are your column names really user_name and user_password? Make sure that your columns are properly escaped if you're using a reserved word
Yes. Here's the SQL Stmt. for the table - CREATE TABLE 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;

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.