I'm trying to convert from ResultSet to CachedRowSet/CachedRowSetImpl. The ResultSet seems to be empty after the populate method, but so does the CachedRowSet. I have been searching all over the place, trying different approaches (including Factory). Below is a code snippet with some indications of what is going on.
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;
}
}
Any help or insight to this problem will be greatly appreciated!
EDIT: Through some debugging, I found that the CachedRowSet is not empty. The RowSetMD.colCount = 3. It also has the right labels. This doesn't change the issue, but assures that I'm not calling getters on an empty object. This makes the issue even harder to grasp
The
CachedRowSet::populatemethod reads all rows from yourResultSet. At that point it is no longer possible to callrs.next(). You should usecsr.next().