【问题标题】:ResultSet to CachedRowSetResultSet 到 CachedRowSet
【发布时间】:2018-06-08 07:14:51
【问题描述】:

我正在尝试从 ResultSet 转换为 CachedRowSet/CachedRowSetImpl。在填充方法之后ResultSet 似乎是空的,但CachedRowSet 也是如此。我一直在到处寻找,尝试不同的方法(包括工厂)。下面是一个代码 sn-p,其中包含一些关于正在发生的事情的迹象。

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。这使得问题更难掌握

【问题讨论】:

  • 如果您使用的是Spring-JDBC,请尝试将其添加为标签。

标签: java jdbc resultset jdbi cachedrowset


【解决方案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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-12
    • 2012-01-03
    • 1970-01-01
    相关资源
    最近更新 更多