The JDBC ResultSet is an object that stores the result of a SQL query executed on a database. It maintains a cursor to navigate through rows and allows retrieval of data from each row.
Used to store and process query results
Supports navigation using methods like next(), previous(), first(), and last()
Provides getter methods like getInt(), getString(), and getDouble() to retrieve data
Explanation: DriverManager.getConnection() establishes a connection with the database, and executeQuery() runs the SQL query to return a ResultSet. The rs.next() method is used to iterate through each row of the result. Finally, all resources are closed to prevent memory leaks.
Common Operations with ResultSet
Fetching data from database: We can fetch data from the database based on the requirements by using conditional statements.
Navigating the ResultSet: We can navigate the ResultSet by using methods like next(), previous(), first() and last().
Getting column values: We can fetch column values with specific conditions or without conditions.
Closing the result: Once database operations are completed we need close the connections related to database here we close the ResultSet connection. By using close method.
Types of ResultSet
There are three different characteristics by which ResultSet types are differentiated
1. Scrollability
Determines whether you can move back and forth in the ResultSet
TYPE_FORWARD_ONLY: Can only move forward through the rows
TYPE_SCROLL_INSENSITIVE: Can move forward and backward but changes in the database are not reflected in the ResultSet
TYPE_SCROLL_SENSITIVE: Can move forward and backward but changes are affect the ResultSet
2. Concurrency
Determines whether you can update the ResultSet
CONCUR_READ_ONLY: Can only read data
CONCUR_UPDATABLE: Allows updates to the ResultSet
3. Holdability
Determines what happens to the ResultSet when a Transaction is committed.
HOLD_CURSORS_OVER_COMMIT: The ResultSet remains open after a commit
CLOSE_CURSORS_AT_COMMIT: The ResultSet closes after a commit
Categories of Methods in ResultSet
We have different types of Methods are available based on their functionality below we listed them for you reference.
1. Navigating a ResultSet
Basically these methods are allow is to navigating through the ResultSet and we can navigate in different ways, Below We provide those methods to navigate in the ResultSet.
next(): Used to move to the next row in the ResultSet.
previous(): Used for move to previous row in the ResultSet
first(): Used for move to first row in the ResultSet
last(): Used for move to last row in the ResultSet
absolute(int row): Used to move to specific row
relative(int rows): Used for Moves forward or backward by the specified number of rows
beforeFirst(): Used for Positions the cursor before the first row
afterLast(): Used for Positions the cursor after the last row
2. Retrieving Data from a ResultSet
These methods retrieve data from the current row in the ResultSet. And also You can retrieve data by column index or column name.
getInt(int columnIndex): Used for Retrieves an integer from the specified column
getString(int columnIndex): Used for Retrieves a string from the specified column
getDouble(int columnIndex): Used for Retrieves a double from the specified column
getBoolean(int columnIndex): Used for Retrieves true or false from the specified column
getDate(int columnIndex): Used for Retrieves a java.sql.Date
getObject(int columnIndex): Used for Retrieves any type of object
getArray(int columnIndex): Used for Retrieves a SQL array
3. Updating Data in a ResultSet
These methods allow you to update data in the Result.
updateInt(int columnIndex, int x): Used for Updates an integer value in the specified column
updateString(int columnIndex, String x): used for Updates a string value
updateBoolean(int columnIndex, boolean x): used for Updates a boolean value
updateRow(): used for Updates a row
deleteRow(): used for delete a row
Example: Employees Table
We create a employees table in work database. Below we provide table structure
In this example we perform CRUD operations by using ResultSet. After running this program as java application It show four options to you. Need to select 1 to 4 based on your requirement below I provide the example with related images.
Explanation of the above Program:
Database Connection: Establishes connection using DriverManager with database URL, username and password.
Insert Record: Uses PreparedStatement with placeholders to insert new employee data safely (avoiding SQL injection).
Update Record: Updates existing employee details by matching employee ID.
Fetch All Records: Executes SELECT query, stores results in ResultSet and iterates through rows to display data.
Delete Record: Removes employee record based on employee ID.
Main Function: Provides a console-based menu (via switch) to select and perform CRUD operations.