What is the ResultSet interface in Java?
Introduction
The ResultSet interface in the java.sql package in Java represents the result of a database query as a tabular data set. It allows us to retrieve data from a database, store it in memory, and insert, update and delete data in the database.
Types of ResultSet
A ResultSet object maintains a cursor that points to its current row of data. Based on the cursor movement, the following are the two types of ResultSet:
- Forward-only: This cursor can only move through the
ResultSetobject from the beginning to the end. - Scrollable: This cursor can only move forward and backward through the
ResultSetobject, and jump to specific rows.
Thread safety
The ResultSet object is not thread-safe, which means that it cannot be shared between multiple threads. Each thread must create its own ResultSet instance.
Methods
Below is the list of commonly used methods of the ResultSet interface:
| Method | Description |
|---|---|
next() |
Moves the cursor to the next row of data |
previous() |
Moves the cursor to the previous row of data |
first() |
Moves the cursor to the first row of data |
last() |
Moves the cursor to the last row of data |
getRow() |
Returns the current row number |
beforeFirst() |
Moves the cursor before the first row of data |
afterLast() |
Moves the cursor after the last row of data |
isBeforeFirst() |
Returns true if the cursor is before the first row |
isAfterLast() |
Returns true if the cursor is after the last row |
isFirst() |
Returns true if the cursor is on the first row |
isLast() |
Returns true if the cursor is on the last row |
getInt() |
Retrieves the value of a column as int |
getLong() |
Retrieves the value of a column as long |
getFloat() |
Retrieves the value of a column as float |
getDouble() |
Retrieves the value of a column as double |
getString() |
Retrieves the value of a column as string |
getBoolean() |
Retrieves the value of a column as boolean |
getDate() |
Retrieves the value of a column as a java.sql.Date object |
getTime() |
Retrieves the value of a column as a java.sql.Time object |
getTimestamp() |
Retrieves the value of a column as a java.sql.Timestamp object |
getObject() |
Retrieves the value of a column as an object |
findColumn() |
Returns the index of a given column name |
Code
import java.sql.*;public class Main {static final String DB_URL = "jdbc:mysql://localhost:3306/employees"; // Replacing with our database connection URLstatic final String USER = "root"; // Replacing with our database usernamestatic final String PASSWORD = ""; // Replacing with our database usernamepublic static void main(String[] args) {try (Connection con = DriverManager.getConnection(DB_URL, "root", "");Statement stmt = con.createStatement();ResultSet rs = stmt.executeQuery("SELECT id,name FROM employees");) {while(rs.next())System.out.println(rs.getInt(1)+" "+rs.getString(2));} catch (SQLException e) {System.out.println("SQLException -> " + e.getMessage());}}}
Explanation
We use the code above to retrieve data from a database. We need to have a connection to the database and a statement. The Statement class is used to execute the query. The ResultSet object stores the data from the query.
There are many ways to iterate through a ResultSet object. We can iterate through the ResultSet object using a while loop or an enhanced for loop. The most commonly used option is a while loop.
The example above shows how to retrieve data from a ResultSet object using a while loop. The ResultSet object has methods to retrieve data by the column number or column name.