我正在使用java 7和jdbc模板从postgresql查询整数数组。我的代码如下:
@Autowired
private JdbcTemplate jdbcTemp;
String SQL = "select item_list from public.items where item_id=1";
List<Integer> ListOfitems=jdbcTemp.queryForList(SQL , Integer.class);
我的 item_list 列是 postgresql 中的整数[]。但是当我尝试这样做时,它会抛出一个错误:类型 int psql 异常的错误值。
感谢任何帮助
最佳答案
您可以使用java.sql.Array 。
如果你只想获取整数数组,你可以这样尝试
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());
或者使用RowMapper
Foo foo = template.queryForObject(SQL, new RowMapper<Foo>(){
@Override
public Foo mapRow(ResultSet rs, int rowNum) throws SQLException {
Foo foo = new Foo();
foo.setName(rs.getString("name"));
foo.setIntegers(Arrays.asList((Integer[]) rs.getArray("item_list").getArray()));
return foo;
}
});
Foo 类:
class Foo {
private String name;
private List<Integer> integers;
public String getName() {
return name;
}
// ...
}
关于java - 如何从结果映射整数列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45416597/