Advertisement
Google Ad Slot: content-top
JDBC RowSet
A RowSet in JDBC is an enhanced version of ResultSet that provides additional features such as scrollability, update capabilities, and offline processing.
Why Use RowSet?
✔ Works offline (disconnected from the database).
✔ Provides scrolling and updatable capabilities.
✔ Can be used with JavaBeans (Serializable).
✔ More flexible than ResultSet.
Types of RowSet in JDBC
There are five types of RowSet in Java:
RowSet Type |
Description |
|---|---|
JdbcRowSet |
A connected RowSet, works like ResultSet. |
CachedRowSet |
A disconnected RowSet, can be serialized and modified offline. |
WebRowSet |
A CachedRowSet with XML support (read/write XML). |
FilteredRowSet |
A CachedRowSet that allows filtering rows. |
JoinRowSet |
Allows joining multiple RowSets like SQL JOIN. |
JdbcRowSet:
JdbcRowSet is a connected RowSet that works like a ResultSet but with additional features.
✔ Uses RowSetProvider.newFactory().createJdbcRowSet() to create a JdbcRowSet.
✔ Executes the query using execute().
✔ Iterates through the result using next().
✔ Unlike ResultSet, it is scrollable and updatable.
CachedRowSet:
CachedRowSet allows working offline, meaning once the data is fetched, the database connection is closed, and changes can be made in memory.
✔ Fetches data and disconnects from DB using execute().
✔ Modifies data offline using updateString() and updateRow().
✔ Commits changes back to DB using acceptChanges().
WebRowSet (XML Support):
WebRowSet extends CachedRowSet and allows storing and reading XML format data.
✔ Fetches data and stores it in XML format using writeXml().
✔ Useful for web applications and configuration storage.
FilteredRowSet (Filter Data in Memory):
FilteredRowSet is a CachedRowSet that allows filtering rows based on conditions.
✔ Uses setFilter() to filter data in memory without modifying SQL query.
✔ Only shows employees with id > 100.
JoinRowSet (Joining Multiple RowSets):
JoinRowSet allows joining multiple RowSets like SQL JOIN.
✔ Joins two CachedRowSets on id and emp_id.