Which Statement method is used to execute a SELECT query?

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

Which Statement method is used to execute a SELECT query?

Explanation:
When you want to fetch data with a SELECT, you use the method that returns a ResultSet directly, so you can iterate over rows and read columns. The method designed for this in the API is executeQuery, which takes the SQL and returns a ResultSet containing the query results. This makes it the natural choice for SELECT statements. The other options serve different purposes: execute is a general-purpose runner that returns a boolean telling you whether the first result is a ResultSet; you’d then call getResultSet to obtain it, which is less direct for a simple SELECT. executeUpdate is meant for statements that change data (like INSERT, UPDATE, DELETE) or DDL, and it returns the number of affected rows. executeScalar isn’t part of Java’s standard JDBC; the usual pattern if you want a single value is to run executeQuery and extract the single value from the ResultSet.

When you want to fetch data with a SELECT, you use the method that returns a ResultSet directly, so you can iterate over rows and read columns. The method designed for this in the API is executeQuery, which takes the SQL and returns a ResultSet containing the query results. This makes it the natural choice for SELECT statements. The other options serve different purposes: execute is a general-purpose runner that returns a boolean telling you whether the first result is a ResultSet; you’d then call getResultSet to obtain it, which is less direct for a simple SELECT. executeUpdate is meant for statements that change data (like INSERT, UPDATE, DELETE) or DDL, and it returns the number of affected rows. executeScalar isn’t part of Java’s standard JDBC; the usual pattern if you want a single value is to run executeQuery and extract the single value from the ResultSet.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy