Disconnected data refers to database data that is retrieved and stored locally so that the application can work with the data without maintaining a continuous connection to the database. In JDBC, CachedRowSet is commonly used to work with disconnected data.
A CachedRowSet retrieves records while a database connection is available and stores those records in memory. After the data is loaded, the database connection can be closed, and the application can continue to read, navigate, and modify the data. The RowSet can later reconnect to the database to synchronize changes.
Advantages of Disconnected Data
- Reduces the time a database connection remains open.
- Allows data to be processed while offline or without continuous connectivity.
- Reduces database connection usage.
- Makes data easier to transfer between application components.
- Supports disconnected applications and distributed systems.
Program: Working with Disconnected Data Using CachedRowSet
This program retrieves employee records from the MySQL database using CachedRowSet. After retrieving the data, the database connection is closed. The program then displays the employee records from the CachedRowSet, demonstrating that the data can be processed without keeping the database connection open.
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.RowSetProvider;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DisconnectedDataExample {
public static void main(String[] args) {
String url =
"jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "password";
try {
// Create CachedRowSet
CachedRowSet rowSet =
RowSetProvider.newFactory()
.createCachedRowSet();
// Establish database connection
Connection con =
DriverManager.getConnection(
url, username, password);
// Configure CachedRowSet
rowSet.setCommand(
"SELECT employee_id, employee_name, " +
"department, salary FROM employee");
// Set connection properties
rowSet.setUsername(username);
rowSet.setPassword(password);
// Execute query and retrieve data
rowSet.execute(con);
// Close database connection
con.close();
System.out.println(
"Database Connection Closed.");
System.out.println();
System.out.println(
"Employee Data from CachedRowSet");
System.out.println(
"--------------------------------");
// Process data without database connection
while (rowSet.next()) {
System.out.println(
"Employee ID: " +
rowSet.getInt("employee_id"));
System.out.println(
"Employee Name: " +
rowSet.getString("employee_name"));
System.out.println(
"Department: " +
rowSet.getString("department"));
System.out.println(
"Salary: " +
rowSet.getDouble("salary"));
System.out.println(
"--------------------------------");
}
// Close RowSet
rowSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" DisconnectedDataExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" DisconnectedDataExample
Database Connection Closed.
Employee Data from CachedRowSet
--------------------------------
Employee ID: 201
Employee Name: Arun
Department: IT
Salary: 52000.0
--------------------------------
Employee ID: 202
Employee Name: Priya
Department: HR
Salary: 45000.0
--------------------------------
Employee ID: 203
Employee Name: Kiran
Department: Finance
Salary: 48000.0
--------------------------------Code language: CSS (css)
