mac
mac

Reputation: 85

Getting IncorrectResultSetColoumnCount exception in queryForList

I am using queryForList to get a list from DB ,

my code looks like,

List<RoleIdBean> role = jdbcTemplate.queryForList(query , new Object[] {userId},RoleIdBean.class);

query = select * from role where userid=?

role table has two coloumns and roleIdBean has two variables .

When I am running this code it is saying expected 1, actual 2

Could someone please check where i am going wrong and assist how to use this method.

Upvotes: 0

Views: 276

Answers (1)

Bohuslav Burghardt
Bohuslav Burghardt

Reputation: 34776

As M. Deinum mentions, you have to provide implementation of RowMapper interface so that Spring knows which columns from your table to map to which properties of your object (RoleIdBean). For instance like this:

List<RoleIdBean> list = jdbcTemplate.query("SELECT * FROM role_id", new Object[]{ userId }, new RowMapper<RoleIdBean>() {
    @Override
    public RoleIdBean mapRow(ResultSet rs, int rowNum) throws SQLException {
        RoleIdBean bean = new RoleIdBean();
        // Set properties from the ResultSet, e.g:
        // bean.setRole(rs.getString(1));
        return bean;
    }
});

Upvotes: 1

Related Questions