🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: SQL Injection: all 10 questions

Quiz: SQL Injection · Question 7 of 10

Which JDBC code is safe?

Which JDBC code is safe?

  1. A.stmt.executeQuery("SELECT * FROM users WHERE id = " + id)
  2. B.conn.prepareStatement("SELECT * FROM users WHERE id = ?") followed by ps.setInt(1, id)
  3. C.stmt.executeQuery(String.format("SELECT * FROM users WHERE id = %s", id))
  4. D.Removing quotes from id and concatenating
Answer & explanation

Correct answer: conn.prepareStatement("SELECT * FROM users WHERE id = ?") followed by ps.setInt(1, id)

PreparedStatement sends the value separately from the SQL.

Can you get the rest right without peeking?

Take the timed Quiz: SQL Injection quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →