JoinRowSet is a disconnected RowSet implementation that allows multiple RowSet objects to be combined using join conditions. It provides functionality similar to SQL joins while working with RowSet objects.
Key Features
- Combines multiple RowSet objects.
- Supports join operations.
- Works without requiring a continuous database connection.
- Can combine data from different RowSet sources.
- Useful when related data is already available in separate RowSets.
Program Using JoinRowSet
JoinRowSet allows multiple RowSets to be combined using a common column, similar to a SQL JOIN. In this example, two separate RowSets are created and joined using the department column.
For this program, create an additional table:
CREATE TABLE department (
department_name VARCHAR(50),
manager_name VARCHAR(100)
);
INSERT INTO department
VALUES
('IT', 'Suresh'),
('HR', 'Kavitha'),
('Finance', 'Ramesh');Code language: JavaScript (javascript)
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.JoinRowSet;
import javax.sql.rowset.RowSetProvider;
import java.sql.SQLException;
public class JoinRowSetExample {
public static void main(String[] args) {
try {
// Create first CachedRowSet
CachedRowSet employeeRowSet =
RowSetProvider.newFactory()
.createCachedRowSet();
employeeRowSet.setUrl(
"jdbc:mysql://localhost:3306/jdbc_demo");
employeeRowSet.setUsername("root");
employeeRowSet.setPassword("password");
employeeRowSet.setCommand(
"SELECT employee_id, employee_name, " +
"department FROM employee");
employeeRowSet.execute();
// Create second CachedRowSet
CachedRowSet departmentRowSet =
RowSetProvider.newFactory()
.createCachedRowSet();
departmentRowSet.setUrl(
"jdbc:mysql://localhost:3306/jdbc_demo");
departmentRowSet.setUsername("root");
departmentRowSet.setPassword("password");
departmentRowSet.setCommand(
"SELECT department_name, manager_name " +
"FROM department");
departmentRowSet.execute();
// Create JoinRowSet
JoinRowSet joinRowSet =
RowSetProvider.newFactory()
.createJoinRowSet();
// Add first RowSet
joinRowSet.addRowSet(
employeeRowSet,
"department");
// Add second RowSet
joinRowSet.addRowSet(
departmentRowSet,
"department_name");
System.out.println(
"Employee and Department Details");
System.out.println(
"-----------------------------------------");
while (joinRowSet.next()) {
System.out.println(
"Employee ID: " +
joinRowSet.getInt("employee_id"));
System.out.println(
"Employee Name: " +
joinRowSet.getString("employee_name"));
System.out.println(
"Department: " +
joinRowSet.getString("department"));
System.out.println(
"Manager: " +
joinRowSet.getString("manager_name"));
System.out.println(
"-----------------------------------------");
}
joinRowSet.close();
employeeRowSet.close();
departmentRowSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" JoinRowSetExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" JoinRowSetExample
Employee and Department Details
-----------------------------------------
Employee ID: 112
Employee Name: Raghu
Department: Finance
Manager: Ramesh
-----------------------------------------
Employee ID: 111
Employee Name: Akhila
Department: HR
Manager: Kavitha
-----------------------------------------
Employee ID: 110
Employee Name: Sai
Department: IT
Manager: Suresh
-----------------------------------------
Employee ID: 109
Employee Name: Kiran
Department: Finance
Manager: Ramesh
-----------------------------------------
Employee ID: 108
Employee Name: Anita
Department: HR
Manager: Kavitha
-----------------------------------------
Employee ID: 107
Employee Name: Suresh
Department: IT
Manager: Suresh
-----------------------------------------
Employee ID: 106
Employee Name: Chakrapani
Department: Finance
Manager: Ramesh
-----------------------------------------Code language: CSS (css)
