JDBC RowSet

The instance of RowSet is the java bean component because it has properties and java bean notification mechanism. It is introduced since JDK 5.

It is the wrapper of ResultSet. It holds tabular data like ResultSet but it is easy and flexible to use.

The implementation classes of RowSet interface are as follows:

  • JdbcRowSet
  • CachedRowSet
  • WebRowSet
  • JoinRowSet
  • FilteredRowSet
Java Rowset

Let's see how to create and execute RowSet.

Example
snippet
JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
        rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
        rowSet.setUsername("system");
        rowSet.setPassword("oracle");
                 
        rowSet.setCommand("select * from emp400");
        rowSet.execute();
Note
It is the new way to get the instance of JdbcRowSet since JDK 7.

Advantage of RowSet

The advantages of using RowSet are given below:

  1. It is easy and flexible to use
  2. It is Scrollable and Updatable bydefault

Simple example of JdbcRowSet

Let's see the simple example of JdbcRowSet without event handling code.

Example
snippet
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;

public class RowSetExample {
        public static void main(String[] args) throws Exception {
                 Class.forName("oracle.jdbc.driver.OracleDriver");
	
	//Creating and Executing RowSet
        JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
        rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
        rowSet.setUsername("system");
        rowSet.setPassword("oracle");
                 
        rowSet.setCommand("select * from emp400");
        rowSet.execute();
                 
	while (rowSet.next()) {
                        // Generating cursor Moved event
                        System.out.println("Id: " + rowSet.getString(1));
                        System.out.println("Name: " + rowSet.getString(2));
                        System.out.println("Salary: " + rowSet.getString(3));
                }
               
        }
}

The output is given below:

Output
Id: 55 Name: Om Bhim Salary: 70000 Id: 190 Name: abhi Salary: 40000 Id: 191 Name: umesh Salary: 50000

Full example of Jdbc RowSet with event handling

To perform event handling with JdbcRowSet, you need to add the instance of RowSetListener in the addRowSetListener method of JdbcRowSet.

The RowSetListener interface provides 3 method that must be implemented. They are as follows:

Example
snippet
1) public void cursorMoved(RowSetEvent event);
2) public void rowChanged(RowSetEvent event);
3) public void rowSetChanged(RowSetEvent event);

Let's write the code to retrieve the data and perform some additional tasks while cursor is moved, cursor is changed or rowset is changed. The event handling operation can't be performed using ResultSet so it is preferred now.

Example
snippet
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;

public class RowSetExample {
        public static void main(String[] args) throws Exception {
                 Class.forName("oracle.jdbc.driver.OracleDriver");
	
	//Creating and Executing RowSet
	JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
	rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
	rowSet.setUsername("system");
	rowSet.setPassword("oracle");
                 
        rowSet.setCommand("select * from emp400");
        rowSet.execute();
                 
	//Adding Listener and moving RowSet
	rowSet.addRowSetListener(new MyListener());

                 while (rowSet.next()) {
                        // Generating cursor Moved event
                        System.out.println("Id: " + rowSet.getString(1));
                        System.out.println("Name: " + rowSet.getString(2));
                        System.out.println("Salary: " + rowSet.getString(3));
                }
               
        }
}

class MyListener implements RowSetListener {
      public void cursorMoved(RowSetEvent event) {
                System.out.println("Cursor Moved...");
      }
     public void rowChanged(RowSetEvent event) {
                System.out.println("Cursor Changed...");
     }
     public void rowSetChanged(RowSetEvent event) {
                System.out.println("RowSet changed...");
     }
}

The output is as follows:

Output
Cursor Moved... Id: 55 Name: Om Bhim Salary: 70000 Cursor Moved... Id: 190 Name: abhi Salary: 40000 Cursor Moved... Id: 191 Name: umesh Salary: 50000 Cursor Moved...
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +