我对 next()、previous() 和 absolute() 中的同一点有疑问 在这些 bool 方法中
如果返回false,光标的位置是否改变 例如: 1)next() 的实例:
ResultSet rs = new Result();
while (rs.next())
System.out.println(rs.getInt(1));
rs.next(); // it will return false but will the cursor be after after last ?!
2)绝对()的情况:
ResultSet rs = new Result();
rs.absolute(15); //assuming I've only 10 rows it will return false but will the cursor be advanced ?
如果答案是它们仅在返回 true 时才更改,那么为什么有一种方法让我脱离行范围,如 afterLast() ?
顺便说一句,我已经检查了 java 文档,但没有找到问题的答案...
最佳答案
afterLast()
可用于强制耗尽 RestulSet
。如果 ResultSet
是可滚动的,另一种用法是使用 previous()
方法向后迭代它:
ResultSet rs = ...;
rs.afterLast();
while (rs.previous()) {
// do something.
}
诚然,我想不出任何好的理由来这样做,但这是可行的。
编辑:
关于如果在已经位于最后一行之后的 ResultSet
上调用 next()
会发生什么的问题 - 这取决于 JDBC 驱动程序的实现。有些驱动程序只是默默地返回 false
并且不更改 ResultSet
的状态,而有些驱动程序可能会因 SQLException
而导致调用失败。更具体地说,请引用文档:
If the result set type is TYPE_FORWARD_ONLY, it is vendor specified whether their JDBC driver implementation will return false or throw an SQLException on a subsequent call to next.
关于mysql - java.sql.ResultSet.next() 说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27280216/