«Back to Home

Core Java

Topics

RowSet In JDBC

RowSet
 
In JDBC, It is introduced since JDK 5. The object of RowSet is the Java bean component because it has properties and Java bean notification mechanism. It is the wrapper of ResultSet and holds tabular data like ResultSet but it is easy and flexible to use.
 
The implementation classes of RowSet interface are given below.
  • JdbcRowSet

  • CachedRowSet

  • WebRowSet

  • JoinRowSet

  • FilteredRowSet
How to create and execute RowSet
  1. JdbcRowSet r = RowSetProvider.newFactory().createJdbcRowSet();  
  2. r.setUrl("jdbc:derby://localhost:1527/Student");  
  3. r.setUsername("Student");  
  4. r.setPassword("student");  
  5. r.setCommand("select * from Student");  
  6. r.execute();  
This is new way to get the instance of JdbcRowSet since JDK 7.
 
Advantage of RowSet

RowSet is easy and flexible to use.
 
RowSet is scrollable and updatable by default.
 
Let's see an example of JdbcRowSet without event handling the code.
 
Code
  1. import java.sql.*;  
  2. import javax.sql.rowset.JdbcRowSet;  
  3. import javax.sql.rowset.RowSetProvider;  
  4. public class RowSetDatabaseEx {  
  5.     public static void main(String args[]) throws Exception {  
  6.   
  7.         Class.forName("org.apache.derby.jdbc.ClientDriver");  
  8.         JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();  
  9.         rowSet.setUrl("jdbc:derby://localhost:1527/Student");  
  10.         rowSet.setUsername("Student");  
  11.         rowSet.setPassword("student");  
  12.         rowSet.setCommand("select * from STUDENT.STUDENTDB");  
  13.         rowSet.execute();  
  14.         while (rowSet.next()) {  
  15.             System.out.println("Id: " + rowSet.getString(1));  
  16.             System.out.println("Name: " + rowSet.getString(2));  
  17.             System.out.println("Age: " + rowSet.getString(3));  
  18.             System.out.println("College: " + rowSet.getString(4));  
  19.         }  
  20.     }  
  21. }  
17

Output

18

If we want to perform event handling with JdbcRowSet then, we need to add the object of RowSetListener in the addRowSetListener method of JdbcRowSet.
 
The RowSetListener interface gives three methods, which must be implemented. They are,
  • public void cursorMoved(RowSetEvent event);

  • public void rowChanged(RowSetEvent event);

  • public void rowSetChanged(RowSetEvent event);
Now, let’s see an example of Jdbc RowSet with the event handling.
 
Code
  1. import java.sql.*;  
  2. import javax.sql.RowSetEvent;  
  3. import javax.sql.RowSetListener;  
  4. import javax.sql.rowset.JdbcRowSet;  
  5. import javax.sql.rowset.RowSetProvider;  
  6.   
  7. public class RowSetDatabaseEx {  
  8.     public static void main(String args[]) throws Exception {  
  9.         Class.forName("org.apache.derby.jdbc.ClientDriver");  
  10.         JdbcRowSet r = RowSetProvider.newFactory().createJdbcRowSet();  
  11.         r.setUrl("jdbc:derby://localhost:1527/Student");  
  12.         r.setUsername("Student");  
  13.         r.setPassword("student");  
  14.         r.setCommand("select * from STUDENT.STUDENTDB");  
  15.         r.execute();  
  16.         r.addRowSetListener(new MyListener());  
  17.         while (r.next()) {  
  18.             System.out.println("Id: " + r.getString(1));  
  19.             System.out.println("Name: " + r.getString(2));  
  20.             System.out.println("Age: " + r.getString(3));  
  21.             System.out.println("College: " + r.getString(4));  
  22.         }  
  23.     }  
  24. }  
  25. class MyListenerClass implements RowSetListener {  
  26.     public void cursorMoved(RowSetEvent event) {  
  27.         System.out.println("Cursor Moved");  
  28.     }  
  29.     public void rowChanged(RowSetEvent event) {  
  30.         System.out.println("Cursor Changed");  
  31.     }  
  32.     public void rowSetChanged(RowSetEvent event) {  
  33.         System.out.println("RowSet changed");  
  34.     }  
  35. }  
  36.    
19

20

Output

21

Summary

Thus, we learnt, in JDBC, the object of RowSet is the Java bean component because it has properties and Java bean notification mechanism. It is the wrapper of ResultSet and holds tabular data like ResultSet and also learnt how we can use it in Java.