FilteredRowSet

FilteredRowSet is a disconnected RowSet implementation used to display only the rows that satisfy a particular filtering condition. It extends WebRowSet and allows filtering without requiring a separate SQL query for every filtering operation.

For example, if the RowSet contains all employees, a filter can be used to display only employees from the IT department.

Key Features

  • Works in disconnected mode.
  • Supports filtering of rows.
  • Uses a Predicate object to define filtering rules.
  • Can reduce the amount of data that needs to be processed by the application.
  • The filter can be applied dynamically.

Program Using FilteredRowSet

FilteredRowSet is used to filter rows according to a condition. It works in disconnected mode and uses a Predicate object to determine which rows should be visible.

import javax.sql.RowSet;
import javax.sql.rowset.FilteredRowSet;
import javax.sql.rowset.RowSetProvider;
import javax.sql.rowset.Predicate;

import java.sql.SQLException;

public class FilteredRowSetExample {

    public static void main(String[] args) {

        try {
            // Create FilteredRowSet
            FilteredRowSet rowSet =
                    RowSetProvider.newFactory()
                                  .createFilteredRowSet();

            // Set database properties
            rowSet.setUrl(
                "jdbc:mysql://localhost:3306/jdbc_demo");

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

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

            // Retrieve all data
            rowSet.execute();

            // Create filter
            Predicate filter = new Predicate() {

                public boolean evaluate(
                        RowSet rs) {

                    try {
                        String department =
                            rs.getString("department");

                        return "IT".equalsIgnoreCase(
                            department);

                    } catch (SQLException e) {
                        return false;
                    }
                }

                public boolean evaluate(
                        Object value,
                        int column)
                        throws SQLException {

                    return true;
                }

                public boolean evaluate(
                        Object value,
                        String columnName)
                        throws SQLException {

                    return true;
                }
            };

            // Apply filter
            rowSet.setFilter(filter);

            System.out.println(
                "Employees in IT Department");

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

            // Display filtered records
            while (rowSet.next()) {

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

            rowSet.close();

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

PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" FilteredRowSetExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" FilteredRowSetExample
Employees in IT Department
-----------------------------
107  Suresh  IT  46500.0
110  Sai  IT  45000.0Code language: CSS (css)
Scroll to Top