Home / Learn Secure SQL / Java: JDBC PreparedStatement

Java: JDBC PreparedStatement

The standard Java database API, built around this exact concept.

The gistThe standard Java database API, built around this exact concept.

The idea, explained simply

JDBC patternconnection.prepareStatement(sql)stmt.setString(1,email)stmt.executeQuery()
JDBC pattern

The basic idea

Java's JDBC API has a class literally named PreparedStatement, created with connection.prepareStatement(sql) and filled with setString(), setInt(), and similar typed setters.

Why this comes up

JDBC's naming makes the concept unusually explicit: if you are not using PreparedStatement (using plain Statement with a built string instead), you have deliberately chosen the less safe path.

The mechanics

Always prefer PreparedStatement over Statement when any part of the query depends on outside input; use the typed setXxx methods for each placeholder.

Where it is used

  • Web application code review
  • Backend development
  • Database administration
Java JDBC
String sql = "SELECT * FROM users WHERE email = ?";
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
    stmt.setString(1, email);
    try (ResultSet rs = stmt.executeQuery()) {
        while (rs.next()) { /* read columns */ }
    }
}

Quick check

1. JDBC's class name for this pattern is literally...