All for Joomla All for Webmasters

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

Let’s see how to create and execute RowSet.

  1. JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
  2. rowSet.setUrl(“jdbc:oracle:thin:@localhost:1521:xe”);
  3. rowSet.setUsername(“system”);
  4. rowSet.setPassword(“oracle”);
  5. rowSet.setCommand(“select * from emp400”);
  6. rowSet.execute();

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.

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. import javax.sql.RowSetEvent;
  6. import javax.sql.RowSetListener;
  7. import javax.sql.rowset.JdbcRowSet;
  8. import javax.sql.rowset.RowSetProvider;
  9. public class RowSetExample {
  10.         public static void main(String[] args) throws Exception {
  11.                  Class.forName(“oracle.jdbc.driver.OracleDriver”);
  12.     //Creating and Executing RowSet
  13.         JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
  14.         rowSet.setUrl(“jdbc:oracle:thin:@localhost:1521:xe”);
  15.         rowSet.setUsername(“system”);
  16.         rowSet.setPassword(“oracle”);
  17.         rowSet.setCommand(“select * from emp400”);
  18.         rowSet.execute();
  19.     while (rowSet.next()) {
  20.                         // Generating cursor Moved event
  21.                         System.out.println(“Id: “ + rowSet.getString(1));
  22.                         System.out.println(“Name: “ + rowSet.getString(2));
  23.                         System.out.println(“Salary: “ + rowSet.getString(3));
  24.                 }
  25.         }
  26. }

The output is given below:

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:

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.

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.Statement;
  5. import javax.sql.RowSetEvent;
  6. import javax.sql.RowSetListener;
  7. import javax.sql.rowset.JdbcRowSet;
  8. import javax.sql.rowset.RowSetProvider;
  9. public class RowSetExample {
  10.         public static void main(String[] args) throws Exception {
  11.                  Class.forName(“oracle.jdbc.driver.OracleDriver”);
  12.     //Creating and Executing RowSet
  13.     JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
  14.     rowSet.setUrl(“jdbc:oracle:thin:@localhost:1521:xe”);
  15.     rowSet.setUsername(“system”);
  16.     rowSet.setPassword(“oracle”);
  17.         rowSet.setCommand(“select * from emp400”);
  18.         rowSet.execute();
  19.     //Adding Listener and moving RowSet
  20.     rowSet.addRowSetListener(new MyListener());
  21.                  while (rowSet.next()) {
  22.                         // Generating cursor Moved event
  23.                         System.out.println(“Id: “ + rowSet.getString(1));
  24.                         System.out.println(“Name: “ + rowSet.getString(2));
  25.                         System.out.println(“Salary: “ + rowSet.getString(3));
  26.                 }
  27.         }
  28. }
  29. class MyListener implements RowSetListener {
  30.       public void cursorMoved(RowSetEvent event) {
  31.                 System.out.println(“Cursor Moved…”);
  32.       }
  33.      public void rowChanged(RowSetEvent event) {
  34.                 System.out.println(“Cursor Changed…”);
  35.      }
  36.      public void rowSetChanged(RowSetEvent event) {
  37.                 System.out.println(“RowSet changed…”);
  38.      }
  39. }

The output is as follows:

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...
PinIt
submit to reddit

Leave a Reply

Top