喜欢spring jdbc中的子句

时间:2013-05-30 13:06:07

标签: spring spring-mvc spring-jdbc

@Override
public List<String> getusers(String role) {
    // TODO Auto-generated method stub
    String namecount = "SELECT userName FROM users WHERE userName LIKE ?";
    role="\"%" + role + "%\"";

    List<String> names = jdbcTemplate.query("SELECT userName FROM users where userName like ?", new RowMapper() {
          public Object mapRow(ResultSet resultSet, int i) throws SQLException {
            return resultSet.getString(1);
          }
        },role);
    System.out.println(names);
    return names;
}

我不明白为什么我会收到此错误,请说出错的地方

Error message:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT userName FROM users userName like ?]; nested exception is 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 'like '%blabla%'' at line 1

1 个答案:

答案 0 :(得分:1)

您忘记了WHERE查询中的 JdbcTemplate 关键字。