JdbcRowSet

JdbcRowSet is a connected RowSet implementation that maintains an active connection to the database. It provides a convenient wrapper around a ResultSet and supports both forward and backward navigation depending on its configuration.

Key Features

  • Maintains an active database connection.
  • Supports navigation through rows.
  • Supports updating database records.
  • Provides JavaBeans-style properties and events.
  • Suitable when continuous database connectivity is acceptable.

Program using JdbcRowSet

JdbcRowSet is a connected RowSet implementation. It maintains an active connection with the database while the data is being accessed. This program retrieves employee records using JdbcRowSet and displays them.

import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.RowSetProvider;
import java.sql.SQLException;

public class JdbcRowSetExample {

    public static void main(String[] args) {

        try {
            // Create JdbcRowSet
            JdbcRowSet rowSet =
                    RowSetProvider.newFactory()
                                  .createJdbcRowSet();

            // Database connection properties
            rowSet.setUrl(
                "jdbc:mysql://localhost:3306/jdbc_demo");

            rowSet.setUsername("root");
            rowSet.setPassword("password");

            // SQL query
            rowSet.setCommand(
                "SELECT employee_id, employee_name, " +
                "department, salary FROM employee");

            // Execute query
            rowSet.execute();

            System.out.println(
                "Employee Details");
            System.out.println(
                "-----------------------------");

            // Retrieve records
            while (rowSet.next()) {

                System.out.println(
                    rowSet.getInt("employee_id") + "  " +
                    rowSet.getString("employee_name") + "  " +
                    rowSet.getString("department") + "  " +
                    rowSet.getDouble("salary"));
            }

            // Close RowSet
            rowSet.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
Execution and Output:

PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" JdbcRowSetExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" JdbcRowSetExample
Employee Details
-----------------------------
101  Ravi  Development  78500.0
102  Priya  Operations  71500.0
106  Chakrapani  Finance  204000.0
107  Suresh  IT  46500.0
108  Anita  HR  48500.0
109  Kiran  Finance  52000.0
110  Sai  IT  45000.0
111  Akhila  HR  47000.0
112  Raghu  Finance  52000.0
119  Deepak  Support  36000.0Code language: CSS (css)
Scroll to Top