JDBC ResultSet object types by Santhosh_Mandadi

VIEWS: 27 PAGES: 2

A very useful document on JDBC ResultSet object types

More Info
									                                   http://www.javaservletsjspweb.in
                                       Santhosh Reddy Mandadi

Tuesday, June 19, 2012
Web URL: http://www.javaservletsjspweb.in/2012/06/jdbc-resultset-object-types.html


JDBC ResultSet object types

There're different types of ResultSet behaviors implemented by Javasoft. And here the list of different
behaviors available for ResultSet object
1. Forward only ResultSet
2. Scroll insensitive ResultSet
3. Scroll sensitive ResultSet

1. Forward only ResultSet
A Java application will be able to move the current row position of ResultSet only in the forward
direction by using resultSet.next() method, in case of forward only ResultSet.
By default, a forward only ResultSet will be opened and the Java application is not allowed to use the
methods last(), first(), previous(), absolute(rowNo) on forward only result sets. These operations can be
performed on the scrollable result sets (the next two types)

2. Scroll insensitive ResultSet
Scroll insensitive ResultSet will allow programmers to use last(), first(), previous(), absolute(rowNo)
methods. Scroll insensitive result set can be created by passing extra parameters to createStatement
method. Scroll insensitive resultset is not sensitive to the changes that are made by another application
after opening the ResultSet. Observe below program
ScrollInSensitiveResultSetDemo.java
/**
 * A demo class to explain scroll sensitive resultset
 * @author Santhosh Reddy Mandadi
 * @since 19-Jun-2012
 * @version 1.0
 */

import java.sql.*;

public class ScrollInSensitiveResultSetDemo
{
 public static void main(String[] args) throws Exception
 {
  Class.forName("oracle.jdbc.driver.OracleDriver");
  Connection Connection =
DriverManager.getConnection("jdbc:oracle:@localhost:1521:xe","scott","tiger");
  String vsql;
                                     http://www.javaservletsjspweb.in
                                         Santhosh Reddy Mandadi

  vsql = "SELECT * FROM PRODUCTS";
  Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
  ResultSet resultSet = statement.executeQuery(vsql);
  System.out.println("Current row: "+resultSet.getRow());
  resultSet.absolute(5);
  System.out.println("Current row: "+resultSet.getRow());
  System.out.println("Updating the current 5th row data in DB directly...");
  //This is just to wait the control until we finish the update manually
  System.in.read();System.in.read();
  resultSet.refreshRow();
  System.out.println("**************************************");
 }
}
The above program will create a scroll insensitive resultSet when statement.executeQuery() is executed.
During the above program execution, we're moving to the 5th record directly without invoking
resultSet.next() method 5 times (i.e.; this is the case for forward only result sets). And the control will be
waited for sometime... just update 5th row record in DB manually by you and hit enter after committing
your changes in the DB. When you hit enter, program will execute resultSet.refreshRow() which will
throw an exception indicating that the insensitive result set will not be able to pickup the data freshly
from the database. This is possible with scroll sensitive result set.
3. Scroll sensitive ResultSet
Scroll sensitive result sets behavior is exactly similar to the scroll insensitive result sets and with an
additional functionality of being able to refresh the resultset data that has been updated in the DB by
other applications. We should pass ResultSet.TYPE_SCROLL_SENSITIVE parameter in place of
ResultSet.TYPE_SCROLL_INSENSITIVE in order to create a scroll sensitive result set.

The above program will not raise an error while executing the refreshRow() method if we use scroll
sensitive result set.

								
To top