What is the role of the ResultSet interface in JDBC?

Java MCQ: What is the role of the ResultSet interface in JDBC?

a) To manage database connections
b) To execute SQL queries
c) To retrieve and manipulate query results
d) To manage SQL transactions

Answer:

c) To retrieve and manipulate query results

Explanation:

The ResultSet interface in JDBC is used to retrieve and manipulate the results of SQL queries. When a query is executed using the Statement or PreparedStatement interfaces, the results are returned in a ResultSet object. This object provides methods to navigate through the rows of the result set, retrieve column values, and update the result set if it is updatable.

Here’s an example of using ResultSet:

ResultSet rs = stmt.executeQuery("SELECT * FROM employees");
while (rs.next()) {
    int id = rs.getInt("id");
    String name = rs.getString("name");
    System.out.println("ID: " + id + ", Name: " + name);
}

This example demonstrates how to iterate through the rows of a ResultSet and retrieve values from each column using methods like getInt() and getString().

ResultSet is a key component in JDBC for accessing and processing the data returned by SQL queries.

Reference links:

https://www.rameshfadatare.com/learn-java-programming/
https://www.javaguides.net/p/java-tutorial-learn-java-programming.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top