Tuesday, February 14, 2012

JDBC : Accessing Result Sets - Part I

Posted by on Tuesday, February 14, 2012 Read our previous post
This is my 3rd post on JDBC TUTORIAL. Today we will be learning about how to access the results that we get for a query using ResultSet

Whenever we execute a query to retrieve data from a table using a Java application, the output is stored in a ResultSet object in a tabular format. The ResultSet object maintains a cursor that helps us to move to and fro in the rows stored in the object and the cursor intially points before the first row.

Types of Result Sets

Now we know that the ResultSet object stores the output of a query in a tabular form and there are various types of ResultSet object :

- Read only : Allows us to only read the rows in a ResultSet object.

- Forward only : Allows us to move only in the forward direction i.e. the result set cursor can move only in the forward direction.

- Scrollable : Allows us to move the result set cursor backward and forward.

- Updatalbe : Allows us to update the result set rows retrieved from a database table.

We can specify the type of ResultSet object using the createStatement() method of the Connection interface. This was done in my previous tutorial on JDBC API TUTORIAL.

The table below lists the various fields of the ResultSet interface that we can use :

ResultSet Description
Specifies that the cursor of the ResultSet object is scrollable and it reflects the changes in the data made by other users
Specifies that the cursor of the ResultSet object is scrollable and it DOES NOT reflects the changes in the data made by other users
Specifies that the cursor of the ResultSet object moves in forward direction only from the first row to the last row

The createStatement() method is an overloaded method that has three prototypes. 

- Statement createStatement() : Does not accept any parameter. This create sa default ResultSet object that only allows forward scrolling.

- Statement createStatement(int, int) : Accepts two parameters. The first  parameter  indicated the ResultSet type and the second parameter indicates the concurrency mode for the result set that determines whether the data in the result set can be updated.

- Statement createStatement(int , int, int) : In addition to the ResultSet type and concurrency mode, this method accepts third parameter. This parameter indicates whether or not, the result set is closed after committing data to the database.

© 2010 Code 2 Learn