需要帮忙!我之前在这里问过一个问题 - MySQL/Java 错误 与此相关但不完全相同(不确定 SO 中的此类问题的协议)。我正在使用 JDBC、MySQL 并遇到错误。一、代码
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()});
我需要为 user_name、user_password(2 列)提供默认值,但不知道“create(User user)块中的方式和位置。
这是上面代码的错误:
SEVERE: Servlet.service() for servlet appServlet threw exception
java.sql.SQLException: Column count doesn't match value count at row 1
我已经尝试过没有“默认”、“默认”和只是 (?, ?) 但这给了我另一个错误。
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