Open In App
Related Articles

What is RowSet in Java JDBC?

Improve Article
Save Article
Like Article

RowSet is an interface in java that is present in the java.sql package. Geek do note not to confuse RowSet with ResultSet. 

Note: RowSet is present in package javax.sql while ResultSet is present in package java.sql.   

The instance of RowSet is the java bean component because it has properties and a java bean notification mechanism. It is introduced in JDK5. A JDBC RowSet provides a way to store the data in tabular form. It makes the data more flexible and easier than a ResultSet. The connection between the RowSet object and the data source is maintained throughout its life cycle.

RowSets are classified into five categories based on how they are implemented which are listed namely as below:

  • JdbcRowSet
  • CachedRowSet
  • WebRowSet
  • FilteredRowSet
  • JoinRowSet

The advantage of RowSet is as follows:

  1. It is easy and flexible to use.
  2. It is by default scrollable and can be updated by default whereas ResultSet by default is only forwardable and read-only operation is valid there only.

The JDBC RowSet interface is a RowSet extension. It’s a wrapper for the ResultSet object that adds some extra features.

Syntax: Declaration of Jdbc RowSet interface

public interface JdbcRowSet
extends RowSet, Joinable

In order to connect RowSet with the database, the RowSet interface provides methods for configuring Java bean properties which are depicted below:

void setURL(String url):
void setUserName(String user_name):
void setPassword(String password):

Lastly, we just need to create a JdbcRowSet object where a sample is shown below illustration as follows:


JdbcRowSetrowSet = RowSetProvider.newFactory().createJdbcRowSet();

// 1. Oracle database considered 

// 2. username is set customly as - root 

// 3. Password is set customly as - pass

// 4. Query 
rowSet.setCommand("select * from Students");

Implementation: Assume we have a table named student in the database as:

|  RollNo |   Name   | Marks |
|    1    |   jack   |    92     |
|    2    |   jenny  |    90     | 
|    3    |   mark   |    80     |
|    4    |   joe    |    82     |

Implementing JdbcRowSet and retrieving the records


// Java Program to Illustrate RowSet in JDBC
// Importing database
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;
// Main class
class RowSetDemo {
    // Main driver method
    public static void main(String args[])
        // Try block to check for exceptions
        try {
            // Loading and registering drivers
            // Creating a RowSet
            JdbcRowSetrowSet = RowSetProvider.newFactory()
            // Setting URL, username, password
            // Creating a query
            rowSet.setCommand("select * from Student");
            // Executing the query
            // Processing the results
            while ( {
                // Print and display commands
                System.out.println("RollNo: "
                                   + rowSet.getInt(1));
                System.out.println("Name: "
                                   + rowSet.getString(2));
                System.out.println("Marks: "
                                   + rowSet.getString(3));
        // Catch block to handle the exceptions
        catch (Exception e) {
            // Print and display the exception along with
            // line number using printStackTrace() method




RollNo: 1
Name: jack 
Marks: 92
RollNo: 2
Name: jenny  
Marks: 90
RollNo: 3
Name: mark
Marks: 80
RollNo: 4
Name: joe
Marks: 82


Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 02 Aug, 2022
Like Article
Save Article
Similar Reads
Complete Tutorials