首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ResultSet到CachedRowSet

ResultSet到CachedRowSet
EN

Stack Overflow用户
提问于 2018-06-08 07:14:51
回答 1查看 1.4K关注 0票数 2

我正在尝试从ResultSet转换到CachedRowSet/CachedRowSetImpl。在填充方法之后,ResultSet似乎是空的,但是CachedRowSet也是空的。我一直在到处寻找,尝试不同的方法(包括工厂)。下面是一个代码片段,它显示了正在发生的事情。

代码语言:javascript
复制
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。这使得这个问题更难理解。

EN

回答 1

Stack Overflow用户

发布于 2018-11-18 15:41:30

CachedRowSet::populate方法从ResultSet中读取所有行。在这一点上,不再可能调用rs.next()。您应该使用csr.next()

代码语言:javascript
复制
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;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50755317

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档