What does executeQuery() return when called on a Statement?

Prepare for the Object-Oriented Programming Test with flashcards and multiple choice questions. Each question comes with hints and explanations to enhance understanding. Master OOP concepts and succeed in your exam!

Multiple Choice

What does executeQuery() return when called on a Statement?

Explanation:
The main idea is that executeQuery is the JDBC method you use for SELECT statements, and it returns a ResultSet that represents the tabular data produced by the query. It’s not void, it’s not a single row, and it’s not an update count—the update count is what you get from executeUpdate for INSERT/UPDATE/DELETE. With the ResultSet you can iterate over all returned rows (using next()) and read each column with the appropriate getter methods. If the query yields no rows, the ResultSet is simply empty, but still valid. If you ever need to run a statement that might return either a ResultSet or an update count, you’d use execute, which returns a boolean indicating whether the first result is a ResultSet.

The main idea is that executeQuery is the JDBC method you use for SELECT statements, and it returns a ResultSet that represents the tabular data produced by the query. It’s not void, it’s not a single row, and it’s not an update count—the update count is what you get from executeUpdate for INSERT/UPDATE/DELETE. With the ResultSet you can iterate over all returned rows (using next()) and read each column with the appropriate getter methods. If the query yields no rows, the ResultSet is simply empty, but still valid. If you ever need to run a statement that might return either a ResultSet or an update count, you’d use execute, which returns a boolean indicating whether the first result is a ResultSet.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy