需要 java 中 ResultSet 的帮助

发布于 2024-11-27 02:24:56 字数 444 浏览 0 评论 0 原文

我正在使用 ResultSet 从 SQL 服务器检索数据。代码如下所示:

ResultSet rs = getValueFormTable();

我像这样循环 ResultSet

do{
   // process data
} while(rs.next());

假设有 10 条记录,并且我位于 ResultSet 的第四个值。有一种情况,我需要将第五个 ResultSet 记录的一个值再次返回到第四个 Resultset 记录。

这可能吗?

I am using a ResultSet to retrieve data from my SQL server. The code looks as follows:

ResultSet rs = getValueFormTable();

I'm looping over the ResultSet like this:

do{
   // process data
} while(rs.next());

Suppose there are 10 records and I am at the fourth value of the ResultSet. There comes the situation that I need take one value of the fifth ResultSet record again return back to the fourth Resultset record.

Is that possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人问我粥可暖 2024-12-04 02:24:56

假设有 10 条记录,我位于第四个结果集值中。出现了这样的情况,我需要再次将第五个结果集的一个值返回到第四个结果集。这在java中可能吗?

您需要“可滚动”ResulSet 实例来实现滚动结果集;如果您需要更新以前的结果集,那么您需要可滚动和可更新的结果集(最后一段讨论可更新的结果集)。通常,ResultSet 是 TYPE_FORWARD_ONLY 并且只能使用 next() 方法向前滚动。

您需要创建 ResultSet 实例rel="nofollow">TYPE_SCROLL_INSENSITIVETYPE_SCROLL_SENSITIVE 到调用其他方法,例如 absolute()previous() 来前后移动 ResultSet

创建可滚动的 ResultSet 需要指定 StatementPreparedStatement 对象返回的 ResultSet 类型,作为如前所述,默认类型为 TYPE_FORWARD_ONLY。下面显示了演示如何执行此操作的代码片段:

PreparedStatement pStmt = Connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = pStmt.executeQuery();
...
// You can now invoke rs.previous(), rs.absolute(n) etc. to move back and forth.

您需要阅读如何在 Connection.createStatement, Connection.prepareStatementConnection.prepareCall 方法。

如果您想要修改 ResultSet 的内容而不仅仅是从中读取内容,那么您将需要创建“可更新”ResultSet。通过将 ResultSet 并发类型指定为 CONCUR_UPDATABLE。然后,您可以调用任何 updateXXX 方法,并在其后添加 updateRow 方法来更新底层数据源。

Suppose there are 10 records and i am in fourth ResultSet Value. There comes the situation that i need to need take one value of the fifth ResultSet again return back to the fourth Resultset. Is that possible in java?

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, ResultSets are TYPE_FORWARD_ONLY and can be scrolled only in the forward direction using the next() method.

You will need to create a ResultSet instance of type TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE to invoke other methods like absolute() and previous() for moving back and forth the ResultSet.

Creating a scrollable ResultSet requires you to specify the type of ResultSet returned by the Statement or PreparedStatement object, as the default type is TYPE_FORWARD_ONLY as stated earlier. A snippet demonstrating how to do so is shown below:

PreparedStatement pStmt = Connection.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
ResultSet rs = pStmt.executeQuery();
...
// You can now invoke rs.previous(), rs.absolute(n) etc. to move back and forth.

You would want to read up on how to specific the type of the ResultSet in the Connection.createStatement, Connection.prepareStatement and Connection.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 as CONCUR_UPDATABLE. You can then invoke any of the updateXXX methods and follow them with an updateRow method to update the underlying datasource.

溺深海 2024-12-04 02:24:56

第一个 do-while 并不是从 ResultSet 中获取结果的好方法。我建议您使用 while 循环。

while( rs.next() ){
    ...
}

第二。是的,如果可用的话,有一种方法可以返回到上一行。

if( rs.previous() ){
    //Do what value you want from previous record and jump to next record.
}

First do-while is not a good aproach to get the result out of the ResultSet. I would recoment you to use while Loop.

while( rs.next() ){
    ...
}

Second. yes there is a way to go back to the previous row if it is available.

if( rs.previous() ){
    //Do what value you want from previous record and jump to next record.
}
梦醒灬来后我 2024-12-04 02:24:56

是的,您可以,如果您有 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 return false (but will not throw any exceptions), but any subsequent call to any methods that need the current row, like resultSet.getString will throw an SQLException. You can handle this by checking ResultSet.isFirst

ResultSet has plenty of such methods, have a look at the docs once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文