Skip to content
Sharp

SQL for Java Devs · interview question

In JDBC, how do you ensure that SQL injection is prevented when executing a query that searches for a user by email?

Multiple choiceMediumchapter-8-curatedpremiumsql-for-java-devsindexes
java
PreparedStatement ps = conn.prepareStatement("SELECT * FROM users WHERE email = ?");
ps.setString(1, userEmail);
Choose one answer
Answer and explanation

Correct answer

B. Use PreparedStatement and setString() to bind values.

Why

PreparedStatement allows setting parameters in a query template, which is a safe method to prevent SQL injection. The query is precompiled, and parameters are set separately.

Test yourself on SQL for Java Devs

A free test with instant, explained results. No account needed.

Start free test
All SQL for Java Devs questions →