需要 java 中 ResultSet 的帮助
我正在使用 ResultSet
从 SQL 服务器检索数据。代码如下所示:
ResultSet rs = getValueFormTable();
我像这样循环 ResultSet
:
do{
// process data
} while(rs.next());
假设有 10 条记录,并且我位于 ResultSet
的第四个值。有一种情况,我需要将第五个 ResultSet
记录的一个值再次返回到第四个 Resultset
记录。
这可能吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要“可滚动”
ResulSet
实例来实现滚动结果集;如果您需要更新以前的结果集,那么您需要可滚动和可更新的结果集(最后一段讨论可更新的结果集)。通常,ResultSet 是TYPE_FORWARD_ONLY
并且只能使用next()
方法向前滚动。您需要创建 ResultSet 实例rel="nofollow">
TYPE_SCROLL_INSENSITIVE
或TYPE_SCROLL_SENSITIVE
到调用其他方法,例如absolute()
和previous()
来前后移动ResultSet
。创建可滚动的
ResultSet
需要指定Statement
或PreparedStatement
对象返回的ResultSet
类型,作为如前所述,默认类型为 TYPE_FORWARD_ONLY。下面显示了演示如何执行此操作的代码片段:您需要阅读如何在
Connection.createStatement
,Connection.prepareStatement
和Connection.prepareCall
方法。如果您想要修改 ResultSet 的内容而不仅仅是从中读取内容,那么您将需要创建“可更新”ResultSet。通过将 ResultSet 并发类型指定为
CONCUR_UPDATABLE
。然后,您可以调用任何updateXXX
方法,并在其后添加updateRow
方法来更新底层数据源。You need "scrollable"
ResulSet
instances to achieve to scroll through the resultset; if you need to update the previous resultsets then you need scrollable and updatable resultsets (the last paragraph discusses updatable resultsets). Typically,ResultSet
s areTYPE_FORWARD_ONLY
and can be scrolled only in the forward direction using thenext()
method.You will need to create a
ResultSet
instance of typeTYPE_SCROLL_INSENSITIVE
orTYPE_SCROLL_SENSITIVE
to invoke other methods likeabsolute()
andprevious()
for moving back and forth theResultSet
.Creating a scrollable
ResultSet
requires you to specify the type ofResultSet
returned by theStatement
orPreparedStatement
object, as the default type isTYPE_FORWARD_ONLY
as stated earlier. A snippet demonstrating how to do so is shown below:You would want to read up on how to specific the type of the ResultSet in the
Connection.createStatement
,Connection.prepareStatement
andConnection.prepareCall
methods.If you want to modify the contents of the
ResultSet
and not just read from it, then you will need to create "updatable" ResultSets. This is easily done, by specifying the ResultSet concurrency type asCONCUR_UPDATABLE
. You can then invoke any of theupdateXXX
methods and follow them with anupdateRow
method to update the underlying datasource.第一个
do-while
并不是从ResultSet
中获取结果的好方法。我建议您使用while
循环。第二。是的,如果可用的话,有一种方法可以返回到上一行。
First
do-while
is not a good aproach to get the result out of theResultSet
. I would recoment you to usewhile
Loop.Second. yes there is a way to go back to the previous row if it is available.
是的,您可以,如果您有 TYPE_SCROLL_INSENSITIVE< /a> 或 TYPE_SCROLL_SENSITIVE
ResultSet
使用
ResultSet.previous
(当您位于第一行时,对
ResultSet.previous
的调用将返回false
(但不会抛出任何异常),但对任何需要当前行的方法(例如resultSet.getString
)的任何后续调用都会抛出SQLException
您可以通过检查ResultSet.isFirst
ResultSet 有很多这样的方法,看看docs 一次。
Yes, you can, if you have a TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE
ResultSet
Use
ResultSet.previous
(When you are in the first row, a call to
ResultSet.previous
will returnfalse
(but will not throw any exceptions), but any subsequent call to any methods that need the current row, likeresultSet.getString
will throw anSQLException
. You can handle this by checkingResultSet.isFirst
ResultSet has plenty of such methods, have a look at the docs once.