The ResultSet
interface is a part of the Java Database Connectivity (JDBC) API, which provides a set of Java classes for accessing and manipulating databases. The ResultSet
interface provides methods for accessing and processing the results of a database query.
When a Statement
object is executed using a database query, it returns a ResultSet
object, which contains the results of the query. The ResultSet
object maintains a cursor that points to the current row of data in the result set. The cursor can be moved to navigate through the rows of the result set, and the data in each row can be retrieved using the appropriate methods.
The ResultSet
interface provides methods for retrieving data from the result set based on the data type of the column, such as getInt()
, getString()
, getDouble()
, and so on. It also provides methods for navigating through the result set, such as next()
, previous()
, first()
, last()
, and so on.
In addition, the ResultSet
interface provides methods for updating and deleting rows in the result set, such as updateRow()
, deleteRow()
, and so on. However, not all databases support these methods, and the behavior of these methods may vary depending on the database driver.
Overall, the ResultSet
interface provides a powerful mechanism for retrieving and processing data from a database using Java.
Commonly used methods of ResultSet interface:
Here are some commonly used methods of the ResultSet
interface in Java:
next()
: Moves the cursor to the next row in the result set.getInt(int columnIndex)
: Returns the value of the specified column as anint
in the current row of the result set.getString(int columnIndex)
: Returns the value of the specified column as aString
in the current row of the result set.getDouble(int columnIndex)
: Returns the value of the specified column as adouble
in the current row of the result set.getDate(int columnIndex)
: Returns the value of the specified column as ajava.sql.Date
object in the current row of the result set.getTime(int columnIndex)
: Returns the value of the specified column as ajava.sql.Time
object in the current row of the result set.getTimestamp(int columnIndex)
: Returns the value of the specified column as ajava.sql.Timestamp
object in the current row of the result set.beforeFirst()
: Moves the cursor to before the first row in the result set.afterLast()
: Moves the cursor to after the last row in the result set.first()
: Moves the cursor to the first row in the result set.last()
: Moves the cursor to the last row in the result set.absolute(int row)
: Moves the cursor to the specified row number in the result set.updateRow()
: Updates the current row in the result set with the changes made usingupdateX()
methods.deleteRow()
: Deletes the current row from the result set.close()
: Releases any resources held by theResultSet
object.
These methods provide a powerful mechanism for retrieving and processing data from a database using Java.
Example of Scrollable ResultSet:
A Scrollable ResultSet
allows you to move the cursor to any row in the result set, not just the next row. You can move the cursor forward and backward through the result set, or jump directly to a specific row. Here’s an example of using a Scrollable ResultSet
in Java:
import java.sql.*; public class ScrollableResultSetExample { public static void main(String[] args) throws SQLException { String url = "jdbc:mysql://localhost:3306/mydb"; String user = "root"; String password = "mypassword"; Connection connection = DriverManager.getConnection(url, user, password); Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet resultSet = statement.executeQuery("SELECT * FROM users"); // Move the cursor to the first row resultSet.first(); // Print the data in the first row System.out.println(resultSet.getString("name") + " - " + resultSet.getString("email")); // Move the cursor to the third row resultSet.absolute(3); // Print the data in the third row System.out.println(resultSet.getString("name") + " - " + resultSet.getString("email")); // Move the cursor to the last row resultSet.last(); // Print the data in the last row System.out.println(resultSet.getString("name") + " - " + resultSet.getString("email")); // Move the cursor to the second-to-last row resultSet.previous(); // Print the data in the second-to-last row System.out.println(resultSet.getString("name") + " - " + resultSet.getString("email")); // Close the ResultSet, Statement, and Connection objects resultSet.close(); statement.close(); connection.close(); } }
In this example, we use a Scrollable ResultSet
by passing ResultSet.TYPE_SCROLL_INSENSITIVE
and ResultSet.CONCUR_READ_ONLY
as arguments to the createStatement()
method. We then move the cursor to different rows in the result set using methods like first()
, absolute()
, last()
, and previous()
. Finally, we close the ResultSet
, Statement
, and Connection
objects to release any resources held by them.