Nearby lessons

11 of 37

JDBC - PreparedStatement

📌 What You Will Learn
  • 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:

StageWhat happens
ParsingThe database checks the SQL grammar — is the query correct?
CompilationThe parsed query is converted into an internal executable form.
Optimization + ExecutionThe 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.

In simple words: PreparedStatement compiles the query once and reuses it; Statement recompiles it every time. So when the same query runs many times with different values, PreparedStatement is faster.

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).

Example02
Loading editor…

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 typeMethodExample
intsetInt(index, value)pst.setInt(1, 101)
StringsetString(index, value)pst.setString(2, "Rahul")
doublesetDouble(index, value)pst.setDouble(3, 50000)
DatesetDate(index, date)pst.setDate(4, date)
booleansetBoolean(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.

In simple words: The `?` placeholders in a PreparedStatement are numbered from 1, not 0. setInt(1, ...) fills the first ?, setString(2, ...) fills the second, and so on.
Example03
Loading editor…

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.

Example04
Loading editor…

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.

Trainer's Note: Even if pass = x' OR '1'='1, the database treats it as a plain value — it can never change the SQL meaning. That is why PreparedStatement is both faster and safer — the two reasons every professional uses it.
In simple words: With PreparedStatement the values travel as data, separate from the SQL. Even a tricky input like x' OR '1'='1 is treated as a plain string, so it can never change what the query means.
Example05
Loading editor…

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:

Trainer's Note: Notice the pattern: ? in the query, setXxx(index, value) to fill it, then executeQuery() (for SELECT) or executeUpdate() (for INSERT/UPDATE/DELETE) with no SQL text. The placeholder values are never part of the SQL string.
Example06
Loading editor…
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.