JDBC模板查询

时间:2014-12-09 08:24:18

标签: java spring postgresql jdbc

我正在尝试使用jdbc模板执行此方法:

 public String getClientName(String uuid) {
    System.out.println("UUID here in the dao layer is: " + uuid);
    String sql = "Select email from client where uuid=?";

    return jdbcTemplate.query(sql, new ResultSetExtractor<String>() {

        @Override
        public String extractData(ResultSet rs) throws SQLException,        DataAccessException {
            return rs.getString("email");
        }
    });
}

但是我收到了这个错误:

SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]
org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [Select email from client where uuid=?]; nested exception is org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying =?
Hint: No operator matches the given name and argument type(s). You might need to add explicit type casts.

我哪里错了?

2 个答案:

答案 0 :(得分:2)

您未在任何地方使用uuid参数...我怀疑您确实想要:

return jdbcTemplate.query(sql, new Object[] { uuid },
    new ResultSetExtractor<String>() { ... });

这样uuid将用作SQL中参数的值。

答案 1 :(得分:2)

您没有将您获得的uuid作为参数传递给您的方法。您可以使用api之类的:

Object sqlParameters[] = {uuid};
return jdbcTemplate.query(sql, sqlParameters,
new ResultSetExtractor<List<String>>() {  
    @Override
    public List<String> extractData(ResultSet rs) throws SQLException, DataAccessException {
        List<String> emailList=new ArrayList<String>();  
        while (rs.next()) {
            emailList.add(rs.getString("email"));
        }
        return emailList;
    }});