Nearby lessons
11 of 37JDBC - PreparedStatement
- The life cycle of a SQL query inside the database
- What PreparedStatement is and why it is better than Statement
- Each variation in its own program: Statement vs PreparedStatement INSERT vs SELECT
- SQL injection attack — the vulnerable version and the safe version
- The setXxx methods for filling placeholders
PreparedStatement is a fundamental part of database programming with JDBC. This lesson explains Life Cycle of a SQL Query, Program 1: The Unsafe Way (Statement with values joined) and Program 2: The Safe Way (PreparedStatement INSERT) with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Life Cycle of a SQL Query
When your query reaches the database, it passes through three stages. Knowing this explains why PreparedStatement is faster:
| Stage | What happens |
|---|---|
| Parsing | The database checks the SQL grammar — is the query correct? |
| Compilation | The parsed query is converted into an internal executable form. |
| Optimization + Execution | The database picks the fastest way and runs the query. |
For a Statement, these three stages run every single time you execute the query. For a PreparedStatement, the query is parsed and compiled once, then executed many times with different values — so it is faster when you reuse the same query.
Program 1: The Unsafe Way (Statement with values joined)
With a normal Statement, values are written directly inside the SQL text. This is how beginners (and old code) do it — but it has two problems: the query is recompiled every time, and it is open to SQL injection.
Problems: the query is compiled again every time, and if the values come from user input, joining them with + is dangerous (SQL injection).
Program 2: The Safe Way (PreparedStatement INSERT)
A PreparedStatement is created with the query once, using ? as placeholders. Then we fill the placeholders with setXxx methods and execute.
The setXxx methods — one for each data type
| Placeholder type | Method | Example |
|---|---|---|
| int | setInt(index, value) | pst.setInt(1, 101) |
| String | setString(index, value) | pst.setString(2, "Rahul") |
| double | setDouble(index, value) | pst.setDouble(3, 50000) |
| Date | setDate(index, date) | pst.setDate(4, date) |
| boolean | setBoolean(index, value) | pst.setBoolean(5, true) |
| Object (any) | setObject(index, value) | pst.setObject(6, obj) |
The index is the placeholder's position: the first ? is 1, the second is 2, and so on.
Program 3: PreparedStatement SELECT with a WHERE placeholder
The same idea works for SELECT. The ? in the WHERE clause is filled at run time — no SQL text is joined.
The Safe Login — PreparedStatement Version
The same login written safely with PreparedStatement. Now the values are sent separately as data — they can never change the meaning of the SQL.
Program 4: Full CRUD with PreparedStatement (INSERT + SELECT)
Bring it all together — insert a record from user input, then select records above a salary, all with PreparedStatement:
- Query life cycle: Parsing → Compilation → Optimization + Execution.
- Statement compiles the query every time; PreparedStatement compiles once.
- Separate programs: Statement INSERT, PreparedStatement INSERT, PreparedStatement SELECT.
- SQL injection: user input joined into SQL can change its meaning ('1'='1' always true).
- Compare the UnsafeLogin (vulnerable) and SafeLogin (protected) programs.
- Use PreparedStatement for all value-based SQL in real applications.