开发者

Need help for ResultSet in java

开发者 https://www.devze.com 2023-03-23 15:05 出处:网络
I 开发者_JS百科am using a ResultSet to retrievedata from my SQL server. The code looks as follows:

I 开发者_JS百科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?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号