ResultSet
In JDBC, the ResultSet interface represents the result set of a query. It provides methods to read data returned by a query, typically from a SELECT
statement. A ResultSet
is created by executing a query using the Statement
or PreparedStatement
objects, and it allows navigation and extraction of data from the result.
Types of ResultSet
Forward-only:
- This is the default type, where you can only move the cursor forward through the result set.
- Created with:
Statement statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
Scrollable:
- Allows navigation in both directions, forward and backward, through the result set.
- Created with:
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
Updatable:
- Allows updating the values in the result set.
- Created with:
Statement statement = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
Common Methods of ResultSet
next(): Moves the cursor to the next row in the result set.
while (rs.next()) { System.out.println(rs.getString("name")); }
* **getString()**: Retrieves a column value as a `String`.
```java
String name = rs.getString("name");
getInt(): Retrieves a column value as an
int
.int id = rs.getInt("id");
getBoolean(): Retrieves a column value as a
boolean
.boolean isActive = rs.getBoolean("is_active");
getDate(): Retrieves a column value as a
Date
.Date birthDate = rs.getDate("birth_date");
Example: Using ResultSet to Process Query Results
import java.sql.*;
public class ResultSetExample {
public static void main(String[] args) {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "password");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, name, age FROM users");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("ID: " + id + ", Name: " + name + ", Age: " + age);
}
rs.close();
stmt.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}