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
- JdbcRowSet r = RowSetProvider.newFactory().createJdbcRowSet();
- r.setUrl("jdbc:derby://localhost:1527/Student");
- r.setUsername("Student");
- r.setPassword("student");
- r.setCommand("select * from Student");
- 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
- import java.sql.*;
- import javax.sql.rowset.JdbcRowSet;
- import javax.sql.rowset.RowSetProvider;
- public class RowSetDatabaseEx {
- public static void main(String args[]) throws Exception {
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();
- rowSet.setUrl("jdbc:derby://localhost:1527/Student");
- rowSet.setUsername("Student");
- rowSet.setPassword("student");
- rowSet.setCommand("select * from STUDENT.STUDENTDB");
- rowSet.execute();
- while (rowSet.next()) {
- System.out.println("Id: " + rowSet.getString(1));
- System.out.println("Name: " + rowSet.getString(2));
- System.out.println("Age: " + rowSet.getString(3));
- System.out.println("College: " + rowSet.getString(4));
- }
- }
- }
Output
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
- import java.sql.*;
- import javax.sql.RowSetEvent;
- import javax.sql.RowSetListener;
- import javax.sql.rowset.JdbcRowSet;
- import javax.sql.rowset.RowSetProvider;
- public class RowSetDatabaseEx {
- public static void main(String args[]) throws Exception {
- Class.forName("org.apache.derby.jdbc.ClientDriver");
- JdbcRowSet r = RowSetProvider.newFactory().createJdbcRowSet();
- r.setUrl("jdbc:derby://localhost:1527/Student");
- r.setUsername("Student");
- r.setPassword("student");
- r.setCommand("select * from STUDENT.STUDENTDB");
- r.execute();
- r.addRowSetListener(new MyListener());
- while (r.next()) {
- System.out.println("Id: " + r.getString(1));
- System.out.println("Name: " + r.getString(2));
- System.out.println("Age: " + r.getString(3));
- System.out.println("College: " + r.getString(4));
- }
- }
- }
- class MyListenerClass 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");
- }
- }
Output
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.