简体   繁体   English

在Java中需要有关ResultSet的帮助

[英]Need help for ResultSet in java

I am using a ResultSet to retrieve data from my SQL server. 我正在使用ResultSet从我的SQL Server检索数据。 The code looks as follows: 该代码如下所示:

ResultSet rs = getValueFormTable();

I'm looping over the ResultSet like this: 我像这样遍历ResultSet

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

Suppose there are 10 records and I am at the fourth value of the ResultSet . 假设有10条记录,而我位于ResultSet的第四个值。 There comes the situation that I need take one value of the fifth ResultSet record again return back to the fourth Resultset record. 出现这种情况,我需要再次ResultSet第五个ResultSet记录的一个值,并返回到第四个Resultset记录。

Is that possible? 那可能吗?

Suppose there are 10 records and i am in fourth ResultSet Value. 假设有10条记录,我在第四个ResultSet值中。 There comes the situation that i need to need take one value of the fifth ResultSet again return back to the fourth Resultset. 出现这种情况,我需要将第五个ResultSet的一个值再次返回到第四个结果集。 Is that possible in java? 在Java中有可能吗?

You need "scrollable" ResulSet instances to achieve to scroll through the resultset; 您需要“可滚动” ResulSet实例来实现滚动结果集。 if you need to update the previous resultsets then you need scrollable and updatable resultsets (the last paragraph discusses updatable resultsets). 如果需要更新以前的结果集,则需要可滚动和可更新的结果集(最后一段讨论可更新的结果集)。 Typically, ResultSet s are TYPE_FORWARD_ONLY and can be scrolled only in the forward direction using the next() method. 通常, ResultSetTYPE_FORWARD_ONLY并且只能使用next()方法向前滚动。

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 . 您将需要创建TYPE_SCROLL_INSENSITIVETYPE_SCROLL_SENSITIVE类型的ResultSet实例,以调用其他方法(如absolute()previous()来来回移动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. 创建可滚动的ResultSet需要指定StatementPreparedStatement对象返回的ResultSet的类型,因为如前所述,默认类型为TYPE_FORWARD_ONLY 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. 您可能想了解如何在Connection.createStatementConnection.prepareStatementConnection.prepareCall方法中指定ResultSet的类型。

If you want to modify the contents of the ResultSet and not just read from it, then you will need to create "updatable" ResultSets. 如果要修改ResultSet的内容而不是仅读取它,则需要创建“可更新”的ResultSet。 This is easily done, by specifying the ResultSet concurrency type as CONCUR_UPDATABLE . 通过将ResultSet并发类型指定为CONCUR_UPDATABLE ,可以轻松完成此操作。 You can then invoke any of the updateXXX methods and follow them with an updateRow method to update the underlying datasource. 然后,您可以调用任何updateXXX方法,并在它们之后使用updateRow方法来更新基础数据源。

Yes, you can, if you have a TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE ResultSet 是的,如果您具有TYPE_SCROLL_INSENSITIVETYPE_SCROLL_SENSITIVE ResultSet ,则可以

Use ResultSet.previous 使用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.previous的调用将返回false (但不会引发任何异常),但是对需要当前行的任何方法的任何后续调用(例如resultSet.getString都将引发SQLException 。可以通过检查ResultSet.isFirst处理此问题

ResultSet has plenty of such methods, have a look at the docs once. ResultSet有很多这样的方法,请看一下文档

First do-while is not a good aproach to get the result out of the ResultSet . 首先do-while并不是从ResultSet结果的好方法。 I would recoment you to use while Loop. 我建议您在while循环中使用。

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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM