gpt4 book ai didi

java - 在几次 next() 调用后从 ResultSet 获取 RowCount

转载 作者:行者123 更新时间:2023-12-02 00:19:58 25 4
gpt4 key购买 nike

我有以下代码:

//rs is a ResultSet from a prepared statement
rs.last();
this.resultCount = rs.getRow();
rs.beforeFirst();

如果我在执行几次 rs.next() 后执行该代码,则 rs.beforeFirst() 是错误的。

所以我的问题是:我怎样才能回到当前位置而不是beforeFirst位置?

最佳答案

问题:“我怎样才能回到当前位置而不是回到第一个位置之前。”

答案:您可以使用resultSet.absolute(rowIndex);

<小时/>

详细说明:
将光标移动到此 ResultSet 对象中的给定行号。
如果行号为正,则光标将移动到相对于结果集开头的给定行号。第一行是第 1 行,第二行是第 2 行,依此类推。

如果给定的行号为负数,则光标将移动到相对于结果集末尾的绝对行位置。例如,调用方法 absolute(-1) 将光标定位在最后一行;调用方法absolute(-2) 将光标移动到倒数第二行,依此类推。

如果指定的行号为零,则光标移动到第一行之前。

<小时/>

但是,您可以在程序中使用 absolumte(rowIndex),例如,

int currRowIndex = resultSet.getRow();
resultSet.last(); // <- can throw if resultSet type is TYPE_FORWARD_ONLY
this.resultCount = resultSet.getRow();
resultSet.absolute(currRowIndex); // <- It will allow you to set cursor position.
<小时/>

警告:当使用last()时,如果结果集类型为TYPE_FORWARD_ONLY,则可能会抛出异常。

last
boolean last() throws SQLException
Moves the cursor to the last row in this ResultSet object.<be>

Returns:
- true if the cursor is on a valid row;
- false if there are no rows in the result set

Throws:
- SQLException: if a database access error occurs; this method is called on a closed result set or the result set type is TYPE_FORWARD_ONLY

- SQLFeatureNotSupportedException: if the JDBC driver does not support
this method

关于java - 在几次 next() 调用后从 ResultSet 获取 RowCount,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58078252/

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