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
Let's see how to create and execute RowSet.Play Video
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:
- It is easy and flexible to use.
- 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
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:
- public void cursorMoved(RowSetEvent event);
- public void rowChanged(RowSetEvent event);
- 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
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