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?
You need “scrollable”
ResulSetinstances 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 areTYPE_FORWARD_ONLYand can be scrolled only in the forward direction using thenext()method.You will need to create a
ResultSetinstance of typeTYPE_SCROLL_INSENSITIVEorTYPE_SCROLL_SENSITIVEto invoke other methods likeabsolute()andprevious()for moving back and forth theResultSet.Creating a scrollable
ResultSetrequires you to specify the type ofResultSetreturned by theStatementorPreparedStatementobject, as the default type isTYPE_FORWARD_ONLYas 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.prepareStatementandConnection.prepareCallmethods.If you want to modify the contents of the
ResultSetand 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 theupdateXXXmethods and follow them with anupdateRowmethod to update the underlying datasource.