Junior — Middle
Can you describe what a ResultSet object is in the JDBC API and what its main purpose is?
sobes.tech AI
Answer from AI
In JDBC API, the ResultSet object represents a table of data obtained as a result of executing an SQL query against a database. Its main purpose is to provide a convenient interface for iterating over and reading the rows of the result.
Main characteristics of ResultSet:
- Allows sequential navigation through the result rows (usually via
next()method). - Provides methods to retrieve data from the current row by column name or index.
- Can be read-only or updatable, depending on settings.
Example usage:
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, name FROM users");
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
System.out.println("User: " + id + ", " + name);
}
rs.close();
stmt.close();
Thus, ResultSet serves for convenient access to data retrieved from the database and is a key element in JDBC operations.