1

我正在尝试从转换ResultSetCachedRowSet/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 is not empty。RowSetMD.colCount = 3。它也有正确的标签。这不会改变问题,但可以确保我不会在空对象上调用 getter。这使得问题更难掌握

4

1 回答 1

1

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;
    }
}
于 2018-11-18T15:41:30.247 回答