Difficulty: Easy
Correct Answer: Load the JDBC driver if needed, obtain a Connection, create a Statement or PreparedStatement, execute the SQL query, process the ResultSet if any, and finally close all JDBC resources.
Explanation:
Introduction / Context:
Executing SQL queries from Java is one of the most common tasks in enterprise applications. JDBC provides a standard sequence of steps that a Java program must follow to connect to a database, send queries, and process results. This question tests whether you understand that typical workflow and can list the key steps in the correct order.
Given Data / Assumptions:
Concept / Approach:
The standard pattern for executing a query with JDBC begins by ensuring that the JDBC driver is loaded and registered with DriverManager or that a DataSource is configured. The program then obtains a Connection by using a JDBC URL and credentials. With the connection, it creates a Statement or PreparedStatement object and uses it to send SQL to the database. For SELECT queries, executeQuery returns a ResultSet that you iterate over to read data. For updates, executeUpdate returns an update count. Finally, you must close ResultSet, Statement, and Connection objects to release resources.
Step-by-Step Solution:
Step 1: Load the JDBC driver class if you are using older JDBC versions, or rely on the driver auto registration mechanism in modern environments.Step 2: Obtain a Connection using DriverManager.getConnection or from a DataSource configured by a container or connection pool.Step 3: Create a Statement or PreparedStatement object from the Connection, depending on whether you have parameters in your SQL.Step 4: Execute the SQL using executeQuery for SELECT statements or executeUpdate for INSERT, UPDATE, and DELETE.Step 5: If you executed a SELECT, iterate over the ResultSet with next, reading column values by index or name, and then close the ResultSet, Statement, and Connection in a finally block or using try with resources.
Verification / Alternative check:
Writing a small JDBC program following these steps confirms the flow. If you omit obtaining a connection or creating a statement, the code cannot compile or will throw exceptions. If you run the program without closing the resources, monitoring tools show open connections and potential leaks. Using try with resources demonstrates the importance of closing connections automatically at the end of the block.
Why Other Options Are Wrong:
Option B suggests modifying JVM configuration files and restarting to execute SQL, which is not how JDBC works. Option C claims you can use only a ResultSet, ignoring the need for a Connection and Statement to produce that ResultSet. Option D proposes creating threads and relying on garbage collection for execution, which has no relation to how queries are sent to the database.
Common Pitfalls:
Common mistakes include not closing statements and connections, not using PreparedStatement for parameterized queries, and mixing business logic with low level JDBC code. Connection pools and frameworks like Spring JDBC or JPA abstract some of these steps, but understanding the underlying sequence remains important for debugging and performance tuning.
Final Answer:
The main JDBC steps are to load the driver if needed, obtain a Connection, create a Statement or PreparedStatement, execute the SQL, process any ResultSet, and close all JDBC resources.
Discussion & Comments