What is RowSet in Java JDBC?
Last Updated :
09 Sep, 2024
RowSet is an interface in java that is present in the javax.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:
- It is easy and flexible to use.
- 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:
Illustration:
JdbcRowSetrowSet = RowSetProvider.newFactory().createJdbcRowSet();
// 1. Oracle database considered
rowSet.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
// 2. username is set customly as - root
rowSet.setUsername("root");
// 3. Password is set customly as - pass
rowSet.setPassword("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
// 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
Class.forName(
"oracle.jdbc.driver.OracleDriver");
// Creating a RowSet
JdbcRowSetrowSet = RowSetProvider.newFactory()
.createJdbcRowSet();
// Setting URL, username, password
rowSet.setUrl(
"jdbc:oracle:thin:@localhost:1521:xe");
rowSet.setUsername("root");
rowSet.setPassword("pass");
// Creating a query
rowSet.setCommand("select * from Student");
// Executing the query
rowSet.execute();
// Processing the results
while (rowSet.next()) {
// 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
e.printStackTrace();
}
}
}
Output:
RollNo: 1
Name: jack
Marks: 92
RollNo: 2
Name: jenny
Marks: 90
RollNo: 3
Name: mark
Marks: 80
RollNo: 4
Name: joe
Marks: 82
Similar Reads
Set in Java
The Set Interface is present in java.util package and extends the Collection interface. It is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface adds a feature that restricts the insertion of duplicat
14 min read
SortedSet tailSet() method in Java
The tailSet() method of SortedSet interface in Java is used to return a view of the portion of this set whose elements are greater than or equal to the parameter fromElement. The set returned by this method is backed by this set, so changes in the returned set are reflected in this set, and vice-ver
2 min read
TreeSet in Java
TreeSet provides an implementation of the SortedSet Interface and SortedSet extends Set Interface. It behaves like simple set with the exception that it stores elements in sorted format. Following are the features of TreeSet. TreeSet uses tree data structure for storage.Objects are stored in sorted,
3 min read
Java JDBC - Difference Between Row Set and Result Set
ResultSet characteristics are as follows: It maintains a connection to a database and because of that, it canât be serialized.it can not pass the Result set object from one class to another class across the network.ResultSet object maintains a cursor pointing to its current row of data. Initially, t
2 min read
Convert List to Set in Java
Java Set is a part of java.util package and extends java.util.Collection interface. It does not allow the use of duplicate elements and at max can accommodate only one null element. Few important features of the Java Set interface are as follows: The set interface is an unordered collection of objec
5 min read
Java.util.BitSet.set() method in Java
There are four variants of set() method.This article depicts about all of them, as follows: 1. set(int Index) : This method sets the bit at the specified index to true i.e adds a value. Declaration : public void set(int bitIndex) Parameters : Index : a bit index. Result : This method does not return
3 min read
Types of Statements in JDBC
In Java, the Statement interface in JDBC (Java Database Connectivity) is used to create and execute SQL queries in Java applications. JDBC provides three types of statements to interact with the database: StatementPrepared StatementCallable Statement1. StatementA Statement object is used for general
5 min read
Difference Between Connected vs Disconnected RowSet in Java JDBC
A RowSet is a wrapper around a ResultSet object. It can be connected, disconnected from the database, and can be serialized. It maintains a JavaBean component by setting the properties. You can pass a RowSet object over the network. By default, the RowSet object is scrollable and updatable. This dia
5 min read
MariaDB INSERT Statement
MariaDB is a famous open-source relational database management system. It is renowned for its performance, scalability, and robust functions. One essential thing about operating with databases is the ability to insert information, and the INSERT statement plays a crucial role in this technique. In t
3 min read
JDBC Result Set
Java Database Connectivity is Java-based technology and that provides a standard API for accessing databases in Java applications. The Key Component of Java Database Connectivity is the ResultSet. JDBC driver allows developers to read and manipulate data from the database. The JDBC ResultSet is Obje
8 min read