2013-05-30 188 views
-2
@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; 
} 

我不理解爲什麼我得到這個錯誤條款,可以請一個說哪裏出了問題像春天JDBC

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 
+0

我不明白爲什麼ia得到那個錯誤 – Ganesh

+0

你的查詢沒有'like'%blabla%'',上面粘貼的代碼是正確的嗎? –

+0

哦,對不起,我粘貼了我的代碼的錯誤部分,我將編輯它 – Ganesh

回答

1

你忘了在JdbcTemplate查詢WHERE關鍵字。

+0

非常感謝 – Ganesh