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:
Let's see how to create and execute RowSet.
JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet(); rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe"); rowSet.setUsername("system"); rowSet.setPassword("oracle"); rowSet.setCommand("select * from emp400"); rowSet.execute();
The advantages of using RowSet are given below:
Let's see the simple example of JdbcRowSet without event handling code.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.sql.RowSetEvent; import javax.sql.RowSetListener; import javax.sql.rowset.JdbcRowSet; import javax.sql.rowset.RowSetProvider; public class RowSetExample { public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); //Creating and Executing RowSet JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet(); rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe"); rowSet.setUsername("system"); rowSet.setPassword("oracle"); rowSet.setCommand("select * from emp400"); rowSet.execute(); while (rowSet.next()) { // Generating cursor Moved event System.out.println("Id: " + rowSet.getString(1)); System.out.println("Name: " + rowSet.getString(2)); System.out.println("Salary: " + rowSet.getString(3)); } } }
The output is given below:
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.
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import javax.sql.RowSetEvent; import javax.sql.RowSetListener; import javax.sql.rowset.JdbcRowSet; import javax.sql.rowset.RowSetProvider; public class RowSetExample { public static void main(String[] args) throws Exception { Class.forName("oracle.jdbc.driver.OracleDriver"); //Creating and Executing RowSet JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet(); rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe"); rowSet.setUsername("system"); rowSet.setPassword("oracle"); rowSet.setCommand("select * from emp400"); rowSet.execute(); //Adding Listener and moving RowSet rowSet.addRowSetListener(new MyListener()); while (rowSet.next()) { // Generating cursor Moved event System.out.println("Id: " + rowSet.getString(1)); System.out.println("Name: " + rowSet.getString(2)); System.out.println("Salary: " + rowSet.getString(3)); } } } class MyListener 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..."); } }
The output is as follows: