jdbcTemplate中的ResultSetExtractor抛出编译错误 [英] ResultSetExtractor in jdbcTemplate throwing compilation error

查看:66
本文介绍了jdbcTemplate中的ResultSetExtractor抛出编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试演示如何使用ResultSetExtractor,但是我无法使其正常工作.

I was trying out a demo on how to use ResultSetExtractor but I am not able to make it work.

下面是我尝试的代码:

public String retrieveDeptName(final int deptId){
    String deptName = (String)jdbcTemplate.update(new PreparedStatementCreator() {

        @Override
        public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {               
            return con.prepareStatement(SELECT_DEPT);
        }
    }, new PreparedStatementSetter() {          
        @Override
        public void setValues(PreparedStatement ps) throws SQLException {
            ps.setInt(1, deptId);
        }
    },new ResultSetExtractor() {
        @Override
        public Object extractData(ResultSet rs) throws SQLException,
                DataAccessException {               
            if (rs.next()) {
              return rs.getLong(2);
            }               
            return null;
        }
    });

    System.out.println(deptName);
}

错误是:

The method update(String, Object[], int[]) in the type JdbcTemplate is not applicable for the arguments (new PreparedStatementCreator(){}, new PreparedStatementSetter(){}, new ResultSetExtractor(){})

尽管很清楚,但我很惊讶链接的工作原理?

Though clear enough, I am surprised how this link works then?

请咨询.

推荐答案

类型为JdbcTemplate的方法update(String, Object[], int[])不适用于参数(new PreparedStatementCreator(){}, new PreparedStatementSetter(){}, new ResultSetExtractor(){})

The method update(String, Object[], int[]) in the type JdbcTemplate is not applicable for the arguments (new PreparedStatementCreator(){}, new PreparedStatementSetter(){}, new ResultSetExtractor(){})

该消息非常清楚.如果您查看用于JdbcTemplate的API ,您可以看到没有 没有采用这三种参数类型的update方法:

The message is pretty clear. If you look at the API for JdbcTemplate, you can see there is no update method that takes those three parameter types:

int update(PreparedStatementCreator psc)
int update(PreparedStatementCreator psc, KeyHolder generatedKeyHolder)
int update(PreparedStatementCreator psc, PreparedStatementSetter pss)          
int update(String sql)
int update(String sql, Object... args)
int update(String sql, Object[] args, int[] argTypes)
int update(String sql, PreparedStatementSetter pss) 


尽管很清楚,但我很惊讶链接的工作原理?

该示例不使用update而不使用update,而是使用query确实带有这些参数的重载.

The example does not use update, it uses query, which does have an overload with those arguments.

 T query(PreparedStatementCreator psc, PreparedStatementSetter pss, ResultSetExtractor<T> rse) 

这篇关于jdbcTemplate中的ResultSetExtractor抛出编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆