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.
3 points to say:
- Always parameterize:
PreparedStatement, JPA named parameters, MyBatis#{}(not${}). PreparedStatementdoesn't cover column names/ORDER BY, string-built JPQL/native queries, or second-order injection.- 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.