Sunday, September 15, 2019

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

No comments:

Post a Comment

How to DROP SEQUENCE in Oracle?

  Oracle  DROP SEQUENCE   overview The  DROP SEQUENCE  the statement allows you to remove a sequence from the database. Here is the basic sy...