Steps to Execute SQL Query with JDBC
Fast recall
Quick revision
Core answer
Connection, 2) create a PreparedStatement with your SQL, 3) bind parameters and execute (executeQuery for SELECT, executeUpdate for INSERT/UPDATE/DELETE), 4) process the ResultSet, 5) close resources — best done automatically with try-with-resources.Whether you know the standard JDBC execution sequence and resource cleanup.
Forgetting to close resources or mixing up executeQuery vs executeUpdate.
Use try-with-resources and PreparedStatement; choose the right execute method.
Interview-ready guide
Interview answer
Executing a SQL query in JDBC follows a consistent five-step recipe.
First, you get a Connection, ideally from a DataSource that's backed by a connection pool, or from DriverManager in simpler setups. Second, you create a statement, and you should use a PreparedStatement with parameterized SQL using question-mark placeholders, both for safety against SQL injection and for performance. Third, you bind your parameters with the setter methods like setInt or setString, and then execute.
The execute method depends on the SQL: executeQuery for a SELECT, which returns a ResultSet, or executeUpdate for an INSERT, UPDATE, or DELETE, which returns the number of affected rows.
Fourth, you process the results by iterating the ResultSet, calling next to move row by row and getInt, getString, and so on to read each column; an important detail is that the cursor starts before the first row, so you must call next before reading. Fifth, you close your resources, and they should be closed in reverse order of opening.
The cleanest way to handle that closing is try-with-resources, where you declare the Connection, PreparedStatement, and ResultSet in the try parentheses, and Java automatically closes them in the correct order even if an exception is thrown. That eliminates the classic resource-leak bug.
Steps to Execute SQL Query with JDBC
Direct Answer
The JDBC steps are: 1) get a Connection, 2) create a PreparedStatement with your SQL, 3) bind parameters and execute (executeQuery for SELECT, executeUpdate for INSERT/UPDATE/DELETE), 4) process the ResultSet, 5) close resources — best done automatically with try-with-resources.
Code Example
String sql = "SELECT id, name FROM users WHERE city = ?";
try (Connection con = dataSource.getConnection(); // 1
PreparedStatement ps = con.prepareStatement(sql)) { // 2
ps.setString(1, "Pune"); // 3 bind
try (ResultSet rs = ps.executeQuery()) { // 3 execute (SELECT)
while (rs.next()) { // 4
System.out.println(rs.getInt("id") + ": " + rs.getString("name"));
}
}
} // 5 auto-close
// for INSERT/UPDATE/DELETE use executeUpdate (returns row count):
// int rows = ps.executeUpdate();
Common Mistakes & Pitfalls
- Not closing resources: leaks connections — use try-with-resources.
executeQueryfor updates: useexecuteUpdatefor INSERT/UPDATE/DELETE.- String-concatenated SQL: use
PreparedStatementparameters, not concatenation. - Reading a column before
rs.next(): the cursor starts before the first row. - Catching
SQLExceptionand swallowing it: log or wrap it; a silent catch hides real failures.
Check your understanding
Answer these without looking back at the article.
- What is the difference between executeQuery and executeUpdate?
- Why use try-with-resources in JDBC?
- Why must you call rs.next() before reading a column?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.