ResultSet is a core interface in JDBC used to store and manipulate data retrieved from a database. It represents a table of data generated by executing a SQL query using a Statement or PreparedStatement.
Click the button to simulate the cursor moving through the ResultSet.
| ID | Name | Dept |
|---|---|---|
| 101 | Alice | IT |
| 102 | Bob | HR |
| 103 | Charlie | Sales |
| 104 | Diana | Admin |
A ResultSet object is obtained after executing a SQL query. The cursor initially points before the first row and moves row by row using next().
// Executing SELECT query to obtain ResultSet
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM students");
// Reading data from ResultSet using while loop
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println(id + " " + name);
}
Each call to rs.next() moves the cursor to the next row. Column values are accessed using getter methods like getInt() and getString().
ResultSet, Statement, and ConnectionPreparedStatement for performance and securitySQLException properlyResultSet