To understand how PreparedStatement prevents SQL injection, we need to understand the steps involved in executing an SQL query.
1. The compilation phase. 2. The execution phase.
Whenever a SQL Server engine receives a query, it must go through the phases below,

Parsing and Normalization Phase: At this point, Query is checked for syntax and semantics. It checks if the link tables and columns used in the query exist or not. He also has many other tasks, but he should not stop in detail.
Compilation phase: at this stage, the keywords used in the query such as select, from, where, etc., are converted to a format that the machine understands. This is the stage when the request is interpreted and the corresponding action is taken. He also has many other tasks, but he should not stop in detail.
Query optimization plan: at this stage, a decision tree is created to find ways to fulfill the request. It determines the number of ways to fulfill the request and the costs associated with each method of performing the request. He chooses the best plan to fulfill the request.
Cache: the best plan selected in terms of query optimization is stored in the cache, so when the next request arrives the next time, it should not go through phases 1, phase 2 and phase 3 again. When the next time request arrives, it will be checked directly in the cache and selected from there to execute.
Execution phase: at this stage, the request is completed, and the data is returned to the user as a ResultSet object.
PreparedStatement API Behavior in the Steps Above
PreparedStatements are not complete SQL queries and contain placeholders (s) that are replaced at run time by the actual data provided by the user.
Whenever any PreparedStatment containing placeholders is passed to the SQL Server engine, it goes through the phases below
- Analysis and Normalization Phase
- Compilation phase
- Query optimizer
- Cache (a compiled request with placeholders is stored in the cache.)
Username UPDATE =? and password =? WHERE id =?
The above request will be parsed, compiled with placeholders as a special treatment, optimized and received by Cached. The request at this stage has already been compiled and converted in a format understandable for the machine. Thus, we can say that the Query stored in the cache is precompiled, and only placeholders should be replaced with data provided by the user.
Now at runtime, when the data provided by the user is provided, the Pre-Compiled Query is selected from the cache, and the replacement ones are replaced by data provided by the user.

(Remember that after the place owners are replaced with user data, the final query has not yet been compiled / interpreted, and the SQL Server engine treats the user data as pure data, not SQL, which needs to be analyzed or compiled again, that is, the beauty of PreparedStatement.)
If the query does not have to go through the compilation phase again, then any data replaced with placeholders is treated as pure data and has no meaning for the SQL Server mechanism, and it directly executes the query.
Note. This is the compilation stage after the analysis phase, which understands / interprets the structure of the request and gives meaningful behavior. In the case of PreparedStatement, the request is compiled only once, and the cached compiled request is always selected to replace user data and execute.
Due to a single compilation function in PreparedStatement, it does not contain a SQL Injection attack.
A detailed explanation can be found here: http://javabypatel.blogspot.in/2015/09/how-prepared-statement-in-java-prevents-sql-injection.html
Jayesh Dec 07 '15 at 4:40 2015-12-07 04:40
source share