Consider this statement:
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM t WHERE id=?");
stmt.setInt(1, id);
The above is considered safe from SQL Injection attacks. Is the one below also safe, knowing that id
is of type int
?
PreparedStatement stmt = connection.prepareStatement("SELECT * FROM t WHERE id=" + id);
If not, what can go wrong?
I can think of two things that might go wrong even if id
is an int and can never be anything else:
id
type to a String
.String
, making that part vulnerable.