Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 11 / Topic 8 of 10

PreparedStatement: parameterized queries, and why they matter

Section 7's Statement is fine for SQL you wrote yourself, with no user input in it. The moment a query includes a value someone else typed — a search box, a login form — gluing that value into a SQL string with + lets the input change the structure of the query, not just its data. This is a real, well-known security hole with a name: SQL injection. Think of a mail-merge letter: if "Dear {name}," is filled in by copy-pasting the recipient's raw text into the template before anything checks it, someone named ", your account has no balance owed. Sincerely, " just rewrote your letter. A PreparedStatement is the mail-merge tool that always treats {name} as data, never as more template.

// VULNERABLE: the value becomes part of the SQL text itself
String unsafeSql = "SELECT name, city FROM members WHERE city = '" + attackerInput + "'";

// SAFE: the SQL shape and the value travel to the database separately
String safeSql = "SELECT name, city FROM members WHERE city = ?";
try (PreparedStatement safe = connection.prepareStatement(safeSql)) {
    safe.setString(1, attackerInput);          // ALWAYS treated as a plain string value
    ResultSet rows = safe.executeQuery();
}

Feeding the exact same attacker-chosen text, Pune' OR '1'='1, through both:

$ javac -cp lib/sqlite-jdbc-3.50.3.0.jar ParameterizedQueryDemo.java
$ java  -cp .:lib/sqlite-jdbc-3.50.3.0.jar ParameterizedQueryDemo
-- VULNERABLE: building SQL with string concatenation --
user typed into the "city" box: Pune' OR '1'='1
SQL actually sent: SELECT name, city FROM members WHERE city = 'Pune' OR '1'='1'
  leaked: Asha (Pune)
  leaked: Ravi (Delhi)
  leaked: Meera (Chennai)
rows returned: 3 (every member, not just Pune)

-- SAFE: the same input, through a PreparedStatement --
SQL shape sent once: SELECT name, city FROM members WHERE city = ?
value bound separately: Pune' OR '1'='1
rows returned: 0 (the literal text never matches a real city)

-- a normal, legitimate lookup with the same PreparedStatement shape --
  matched: Ravi (Delhi)

The concatenated version leaks every row — the attacker's text closed the quote early and added OR '1'='1', which is always true, so the WHERE clause matched nothing and everything. The PreparedStatement version sends the fixed SQL shape (city = ?) once and the value separately; the database treats the whole string, quote marks included, as one literal value to compare against a city column, and nothing in a real city name is ever equal to Pune' OR '1'='1. The third block shows the same prepared SQL working perfectly for a normal lookup — parameterizing a query costs nothing for legitimate input, and closes a real hole for malicious input.

⚠️ There is no "safe enough" string concatenation for SQL. Escaping quotes by hand, blocklisting certain characters, trimming input — none of it is reliably safe, and all of it is unnecessary once PreparedStatement exists. Use ? placeholders for every value that did not come from your own source code, with no exceptions.

Full program: examples/ParameterizedQueryDemo.java.

Done here? The practice set for Part 11 — including the solutions — is on the part page.

Search the guide