I always read the blog Java Revisited and I was reading the article today Why use PreparedStatement in Java JDBC - Example Tutorial . At one point the author talks about parameterization of queries, it is better, safer and more performative to use the parameterization offered by the API than concatenate strings and uses the examples below:
SQL Query 1: PreparedStatement using String concatenation
String loanType = getLoanType();
PreparedStatement pstmt = conn.prepareStatement("select banks from loan where loan_type = " + loanType);
SQL Query 2: Query parameterized using PreparedStatement
PreparedStatement pstmt = conn.prepareStatement("select banks from loan where loan_type = ?");
prestmt.setString(1, loanType);
It explains that when using the parameterization the JDBC driver will check the data and with String concatenated not, it will only execute the sent SQL, ie if we have that famous OR 1 = 1
the query will always return true and there is the famous SQL Injection.
What was not very clear to me is because one is more performative than the other and more, that would be worth it if I have an always static parameter too, for example:
PreparedStatement pstmt = conn.prepareStatement(select * from usuarios where ativo = 'S');
Or it's more performative to use as below:
PreparedStatement pstmt = conn.prepareStatement(select * from usuarios where ativo = ?);
pstmt.setString(1, "S");
Another question, in this case I think the security issue is the same, as there is no parameter passing through the user there is no way to have the attack, right?