In JDBC, what is a ResultSet and how is it used to access the rows returned by a SQL query?

Difficulty: Easy

Correct Answer: A ResultSet is a JDBC object that represents a table like set of rows returned by a SQL query and provides a cursor to read column values row by row.

Explanation:


Introduction / Context:
In Java database programming using JDBC, most real applications need to read data back from the database after executing SELECT queries. The core object that represents this returned data is called a ResultSet. Understanding what a ResultSet is and how it behaves is essential for writing correct, efficient, and maintainable JDBC code in enterprise applications.


Given Data / Assumptions:

  • We are using JDBC to connect a Java program to a relational database.
  • SQL queries such as SELECT are executed through Statement or PreparedStatement objects.
  • The query may return multiple rows and multiple columns.
  • The Java code needs a structured way to read column values from each row sequentially.


Concept / Approach:
A ResultSet is a JDBC object that behaves like a cursor over a virtual table of data returned by the database. When you call executeQuery on a Statement or PreparedStatement, the driver sends the SQL to the database and creates a ResultSet that contains zero or more rows. The ResultSet cursor initially points before the first row. By calling methods such as next, previous, first, or last (depending on ResultSet type), you move the cursor and then read column values using getter methods like getInt, getString, or getDate. This model abstracts away the low level protocol between Java and the database server.


Step-by-Step Solution:
Step 1: Create a Statement or PreparedStatement and call executeQuery with a SELECT statement. Step 2: The driver returns a ResultSet object that represents the rows produced by the query. Step 3: Call rs.next() in a loop to advance the cursor from one row to the next. Step 4: Inside the loop, call methods such as rs.getString("name") or rs.getInt(1) to read column values for the current row. Step 5: After finishing, close the ResultSet and related JDBC resources to free database and driver resources.


Verification / Alternative check:
You can verify how a ResultSet works by printing each row as you iterate with rs.next(). If the query returns zero rows, next immediately returns false and the loop body never executes. Different ResultSet types, such as TYPE_FORWARD_ONLY or TYPE_SCROLL_INSENSITIVE, control how the cursor can move and whether changes in the database are visible, but the basic concept of a cursor over query results remains the same in all cases.


Why Other Options Are Wrong:
Option B is incorrect because configuration properties are typically stored in Property objects or external files, not in ResultSet. Option C is wrong since update counts come from executeUpdate or getUpdateCount, not from ResultSet. Option D is incorrect because network protocols operate at a lower level, while ResultSet is a high level Java abstraction for query results.


Common Pitfalls:
A common mistake is forgetting to close ResultSet, Statement, and Connection objects, which can cause resource leaks and exhaust database connections. Another pitfall is assuming that all ResultSet objects are scrollable or updatable, even though many drivers return forward only, read only ResultSet objects by default. Developers should always check driver capabilities and use try with resources to manage ResultSet and related objects safely.


Final Answer:
A ResultSet in JDBC is a Java object that represents a table like set of rows returned by a SQL query and provides a cursor that allows the program to move through the rows and read column values one row at a time.

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion