RowSet Interface in JDBC

RowSet Interface

  • A JDBC RowSet provides the way to store the data in tabular form. It makes the data more flexible and easier than a ResultSet.
  • The connection between RowSet object and data source is maintained throughout its life cycle.

Types of Implementation

There are five categories of RowSet based on their implemention:
1. JdbcRowSet
2. CachedRowSet
3. WebRowSet
4. FilteredRowSet
5. JoinRowSet

JdbcRowSet Interface

The JdbcRowSet is an extension of RowSet interface. It is a wrapper around the ResultSet object with some additional functionality.

Declaration of JdbcRowSet Interface
public interface JdbcRowSet
         extends RowSet, Joinable

Creating a JdbcRowSet Object
The JdbcRowSetImpl provides an implementation of the default constructor.

JdbcRowSetImpl jrs = new JdbcRowSetImpl();
jrs.setCommand("select * from Student where name = ?");
jrs.setURL("jdbc:oracle:thin:@localhost:1521:XE");     
jrs.setUsername("scott");
jrs.setPassword("tiger");
jrs.setString(1, "Surendra");
jrs.execute();

Example : Implementing JdbcRowSet & retrieving the records

import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.Statement;  
import javax.sql.RowSetEvent;  
import javax.sql.RowSetListener;  
import javax.sql.rowset.JdbcRowSet;  
import javax.sql.rowset.RowSetProvider;
classRowSetDemo
{
    public static void main(String args[])
    {
        try
        {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            JdbcRowSetrowSet = RowSetProvider.newFactory().createJdbcRowSet();
            rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
            rowSet.setUsername("scott");
            rowSet.setPassword("tiger");
            rowSet.setCommand("select * from Student");
            rowSet.execute();
            while(rowSet.next())
            {
                 System.out.println("RollNo: "+ rowSet.getInt(1));
                 System.out.println("Name: "+ rowSet.getString(2));
                 System.out.println("Course: "+ rowSet.getString(3));
            }
        }
        catch(Exception e)
        {
             e.printStackTrace();
        }
    }
}