SEARCH HERE

Tuesday, December 27, 2022

JDBC RowSet

 

JDBC RowSet

An instance of RowSet is the Java bean component because it has properties and Java bean notification mechanism. It is the wrapper of ResultSet. A JDBC RowSet facilitates a mechanism to keep the data in tabular form. It happens to make the data more flexible as well as easier as compared to a ResultSet. The connection between the data source and the RowSet object is maintained throughout its life cycle. The RowSet supports development models that are component-based such as JavaBeans, with the standard set of properties and the mechanism of event notification.

It was in the JDBC 2.0, the support for the RowSet was introduced using the optional packages. But the implementations were standardized for RowSet in the JDBC RowSet Implementations Specification (JSR-114) by the Sun Microsystems that is being present in the JDK (Java Development Kit) 5.0.

The implementation classes of the RowSet interface are as follows:

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

Let's see how to create and execute RowSet.Play Video

  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 by default.

Example of JdbcRowSet

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

FileName: RowSetExample.java

  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

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 the cursor is moved, the cursor is changed, or the rowset is changed. The event handling operation can't be performed using ResultSet, so it is preferred now.

FileName: RowSetExample.java

  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...

0 comments:

Post a Comment

C++

AJAVA

C

E-RESOURCES

LKG, UKG Live Worksheets

Top