How to query for an postgres integer array in spring

I am using Java 7 and JDBC template to query a integer array from PostgreSQL. My code is as below:

private JdbcTemplate jdbcTemp;

String SQL = "select item_list from public.items where item_id=1";
List<Integer> ListOfitems=jdbcTemp.queryForList(SQL , Integer.class);

My item_list column is integer[] in PostgreSQL. But when I try like this it throws an error as:

Bad value for type int psql exception

I also tried:

List<List<Integer>> ListOfitems=jdbcTemp.queryForList(SQL , Integer.class);

But it still throws the same exception.

Any help is appreciated.

After lots of trial and error this made the trick.

List<Array>  ListOfitems=jdbcTemp.queryForList(SQL , java.sql.Array.class);

So basically the postgres array is equal to java sql array type.Hope it helps anyone 🙂

To get the array field of the single row it should be something like:

import java.sql.Array;


final String sqlQuery = "...";
final Array itemListArray = jdbcTemplate.queryForObject(sqlQuery, Array.class);

Please note that the method call may throw the exception:

IncorrectResultSizeDataAccessException – if the query does not return exactly one row, or does not return exactly one column in that row

— JdbcTemplate (Spring Framework 4.3.10.RELEASE API) class, public <T> T queryForObject(String sql, Class<T> requiredType) throws DataAccessException method.

You can use java.sql.Array.

If you want to get only integer array you can try like this (it works if result contains one row):

String SQL = "select item_list from public.items where item_id=1";
Array l = template.queryForObject(SQL, Array.class);
List<Integer> list = Arrays.asList((Integer[]) l.getArray());

Or use RowMapper

Foo foo = template.queryForObject(SQL, new RowMapper<Foo>(){
        public Foo mapRow(ResultSet rs, int rowNum) throws SQLException {
            Foo foo = new Foo();
            foo.setIntegers(Arrays.asList((Integer[]) rs.getArray("item_list").getArray()));
            return foo;

Class Foo:

class Foo {
    private String name;
    private List<Integer> integers;

    public String getName() {
        return name;
    // ...