gpt4 book ai didi

java - 结果集到缓存行集

转载 作者:行者123 更新时间:2023-11-30 06:08:30 24 4
gpt4 key购买 nike

我正在尝试从 ResultSet 转换为 CachedRowSet/CachedRowSetImpl。在执行 populate 方法之后,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 不为空。 RowSetMD.colCount = 3。它也有正确的标签。这不会改变问题,但可以确保我不会在空对象上调用 setter/getter 。这使得这个问题更加难以理解

最佳答案

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;
}
}

关于java - 结果集到缓存行集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50755317/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com