JDBC Result Set in Java
The ResultSet is essentially a table of data where each row represents a record and each column represents a field in the database. The ResultSet has a cursor that points to the current row in the ResultSet and we can able to navigate in ResultSet by using the next(), previous(), first(), and last() methods. We can retrieve data by using different methods like getString(), getInt(), getDouble() and other methods.
In Java, the ResultSet is the Object which is used for holding the result of a database query typically the SQL select statement. And It is the part of JDBC API which is used for interacting with relational databases. The ResultSet allows us over the rows of tables returned by the SQL query and extract a specific column from the SQL query result.
Syntax:
try {
Connection conn = DriverManager.getConnection(url, username, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM your_table");
while (rs.next()) {
// Process the result set
}
rs.close();
stmt.close();
conn.close();
}
catch (SQLException e) {
e.printStackTrace(); // Handle the exception
}
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 able navigating 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
- 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 are not reflect ResultSet
- TYPE_SCROLL_SENSITIVE: Can move forward and backward but changes are affect the ResultSet
- Concurrency: Determines whether you can update the ResultSet
- CONCUR_READ_ONLY: Can only read data
- CONCUR_UPDATABLE: Allows updates to the ResultSet
- 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
Category of Methods in Result Set
We have different types of Methods are available based on their functionality below we listed them for you reference.
i). 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.
Method | Description |
---|---|
next() | used for move 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 |
ii). 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.
Method | Description |
---|---|
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 |
iii). Updating Data in a ResultSet:
These methods allow you to update data in the Result.
Method | Description |
---|---|
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 |
Employees Table Structure:
We create a employees table in work database. Below we provide table structure
JDBC Result Set
Java Database Connectivity is Java-based technology and that provides a standard API for accessing databases in Java applications. The Key Component of Java Database Connectivity is the ResultSet. JDBC driver allows developers to read and manipulate data from the database.
The JDBC ResultSet is Object which represents the result of a SQL Query executed on a database. It acts as a cursor to navigate through the retrieved data, manipulate data, Fetching Specific columns and others. In this article, we will learn more about JDBC Result Set.