Yes The prepared statement here will prevent sql injection. This is because you are using a placeholder (?) In the query. It is important to note the placeholder here.
The following are two examples of prepared statements. The first will not impede sql implementation.
PreparedStatement ps = conn.prepareStatement ("SELECT * FROM users WHERE firstname LIKE" + name);
The above statement, even if prepared, will not impede sql implementation
However, the prepared statement below is good for preventing sql injection.
PreparedStatement ps = conn.prepareStatement ("SELECT * FROM users WHERE firstname LIKE?");
The difference b / w of the first and second operators is that, although the request in the first case is dynamically compiled at run time, in the second case it is precompiled.
This means that malicious user input of the type (a'or'1 '=' 1) can modify the request in the first expression. But the second query, since it is precompiled, will process malicious user input as data, not the sql command.
In a nutshell, Assumed Statements prevent SQL injection if and only if they are used with placeholders and Bind parameters.
vivek
source share