You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TYPE_FORWARD_ONLY类型ResultSet能否调用absolute方法移动行指针?

Can absolute() be used to move the cursor forward on a TYPE_FORWARD_ONLY ResultSet?

Great question—this is a tricky edge case that boils down to how absolute(int row) behaves when row=0 and the specifics of your JDBC driver implementation. Let’s break this down clearly:

The API Docs vs. Real-World Behavior

The Java 7 ResultSet docs state that absolute() throws SQLException if the result set type is TYPE_FORWARD_ONLY, but there’s an easy-to-miss exception here:

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

This description is a bit generalized. The key nuance is that absolute(0) doesn’t violate the "forward-only" constraint for many drivers, making it a special case.

Why the Example Code Doesn’t Throw an Exception

When you call rs.absolute(0), you’re moving the cursor to the position before the first row—this is actually the default initial state of any new ResultSet. For forward-only result sets, drivers like Derby (used in the example) allow this operation because:

  • It doesn’t require "jumping" past the current cursor position; you’re just resetting to the starting point
  • The JDBC specification implicitly permits this edge case, even if the API docs don’t explicitly call it out

When You Will Get an Exception

If you call absolute() with any non-zero value (positive or negative) on a TYPE_FORWARD_ONLY ResultSet, you’ll get the SQLException as promised. For example:

  • rs.absolute(1): Trying to jump directly to the first row (violates forward-only rules, since you haven’t traversed to it sequentially)
  • rs.absolute(-1): Trying to jump to the last row (clearly breaks the forward-only constraint)

Final Takeaway

You can safely use absolute(0) on a TYPE_FORWARD_ONLY ResultSet with most mainstream drivers (Derby, MySQL, PostgreSQL, etc.), but never rely on absolute(n) for any non-zero n—that will always throw an exception per the API contract.

内容的提问来源于stack exchange,提问作者dreinoso

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:11:51