SteelReed

Search

Search posts, questions…

SecurityQuestion 6 / 24

Junior

What is SQL injection, how do you prevent it, and where can it still happen with PreparedStatement?

Short answer

SQL injection happens when input is concatenated straight into SQL and read as code (' OR '1'='1). The main defence is a parameterized query: the driver sends values separately from the statement; what can't be bound goes through a whitelist.

WHERE, INSERT valuecolumn name, ORDER BYLIKEdynamic INWhere does input go?Bind as a parameterWhitelist, unknown is 400Bind + escape % _Generate enough ? marks

3 points to say:

  1. Always parameterize: PreparedStatement, JPA named parameters, MyBatis #{} (not ${}).
  2. PreparedStatement doesn't cover column names/ORDER BY, string-built JPQL/native queries, or second-order injection.
  3. Defence in depth: a least-privilege DB user (no DDL), SAST (Semgrep, SonarQube) to catch concatenation.

Keep reading the deep dive

Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.