Junior — Middle
How do you pass parameters to SQL queries and why do you choose this particular approach?
sobes.tech AI
Answer from AI
To pass parameters in SQL queries, I use prepared statements with parameterization. This helps prevent SQL injection and improves performance for repeated queries.
Example using JDBC:
String sql = "SELECT * FROM users WHERE email = ?";
PreparedStatement pstmt = connection.prepareStatement(sql);
pstmt.setString(1, email);
ResultSet rs = pstmt.executeQuery();
This approach ensures that parameters are properly escaped and do not affect the query structure, enhancing the security and stability of the application.