JDBC Statement Types — Statement, PreparedStatement, CallableStatement
Fast recall
Quick revision
Core answer
Whether you can name the three statement types and when each applies.
Defaulting to Statement and building SQL with string concatenation.
State PreparedStatement is the default for any SQL with input — safer and faster.
Interview-ready guide
Interview answer
Use PreparedStatement for SELECT ... WHERE id = ? and bind the id. Use Statement for a fixed one-off command when no parameters are needed. Use CallableStatement when the database contract is a stored routine with registered output parameters. Stored routines and Statement are not automatically safe: any dynamic SQL constructed from untrusted input still needs parameterization or allow-listed structure.
JDBC Statement Types — Statement, PreparedStatement, CallableStatement
Direct Answer
Statement executes SQL text without bind markers. PreparedStatement represents parameterized SQL and is the normal choice for data values. CallableStatement invokes stored procedures/functions according to driver support and extends PreparedStatement. PreparedStatement may use server preparation or plan reuse, but 'precompiled' is not a universal execution guarantee; its reliable safety comes from bound-value semantics.
Code Example
// Statement — static SQL only (no input)
try (Statement st = con.createStatement();
ResultSet rs = st.executeQuery("SELECT COUNT(*) FROM users")) { /* ... */ }
// PreparedStatement — parameterized, safe (the default)
try (PreparedStatement ps = con.prepareStatement("SELECT * FROM users WHERE id = ?")) {
ps.setInt(1, 42);
try (ResultSet rs = ps.executeQuery()) { /* ... */ }
}
// CallableStatement — stored procedure
try (CallableStatement cs = con.prepareCall("{call get_user(?)}")) {
cs.setInt(1, 42);
cs.execute();
}
Common Mistakes & Pitfalls
- Using
Statementwith concatenated input: opens SQL injection — usePreparedStatement. - Thinking they're unrelated: they form an inheritance chain.
- Reaching for
Statementby habit: preferPreparedStatementfor nearly everything. - Using
CallableStatementfor plain queries: it's for stored procedures.
Check your understanding
Answer these without looking back at the article.
- Why is PreparedStatement preferred over Statement?
- When would you use a CallableStatement?
- How do the three statement types relate by inheritance?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.