Reputation: 117
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
Upvotes: 2
Views: 2508
Reputation: 4162
The CachedRowSet::populate
method reads all rows from your ResultSet
. At that point it is no longer possible to call rs.next()
. You should use 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;
}
}
Upvotes: 2