SQL for Java Devs · interview question
Which of the following is a correct way to use a PreparedStatement to prevent SQL injection in an e-commerce application?
Multiple choiceHardchapter-8-curatedpremiumsql-for-java-devspreparedstatements
Answer and explanation
Correct answer
B. PreparedStatement stmt = connection.prepareStatement("SELECT * FROM users WHERE email =?"); stmt.setString(1, userEmail);
Why
The correct usage involves binding parameters using placeholders ('?') and setting values with methods like setString(). This ensures that input is treated as data, not executable code, thus preventing SQL injection.
Test yourself on SQL for Java Devs
A free test with instant, explained results. No account needed.
More SQL for Java Devs questions
- How can JDBC transactions help maintain data integrity during an e-commerce checkout process?
- What would be the effect of not closing a PreparedStatement in a Java application?
- In JDBC, how do you ensure that SQL injection is prevented when executing a query that searches for a user by email?
- Why do teams use SQL JOINs to fetch relational data in a single query in production code?
- What should a repository method mainly encapsulate?
- How can you prevent the N+1 query problem when retrieving a list of orders with their customer details?