我正在尝试从ResultSet转换到CachedRowSet/CachedRowSetImpl。在填充方法之后,ResultSet似乎是空的,但是CachedRowSet也是空的。我一直在到处寻找,尝试不同的方法(包括工厂)。下面是一个代码片段,它显示了正在发生的事情。
class ResultSetMapper implements RowMapper<CachedRowSet>{
@Override
public CachedRowSet map(ResultSet rs, StatementContext ctx) throws SQLException {
//CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet();
System.out.println(rs.getLong("something")); -> This gets printed
CachedRowSetImpl crs = new CachedRowSetImpl();
crs.populate(rs);
System.out.println(crs.getInt("something"); -> ArrayIndexOutOfBoundsException (mostly -1, sometimes returning 0)
System.out.println(rs.getLong("something")); -> This doesn't get printed
System.out.println(crs.size()); -> 0
return crs;
}
}任何对这个问题的帮助或洞察力都将受到极大的感谢!
编辑:通过一些调试,我发现CachedRowSet是而不是空。RowSetMD.colCount = 3。它也有正确的标签。这不会改变问题,但确保我不会在空对象上调用getter。这使得这个问题更难理解。
发布于 2018-11-18 15:41:30
CachedRowSet::populate方法从ResultSet中读取所有行。在这一点上,不再可能调用rs.next()。您应该使用csr.next()。
class ResultSetMapper implements RowMapper<CachedRowSet>{
@Override
public CachedRowSet map(ResultSet rs, StatementContext ctx) throws SQLException {
CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet();
crs.populate(rs);
while (csr.next()) {
System.out.println(crs.getInt("something"));
}
// ...
return null;
}
}https://stackoverflow.com/questions/50755317
复制相似问题