Search code examples
javasqljdbcprecompile

What is pre-compiled SQL statement?


PreparedStatement is a precompiled statement so it will increase jdbc performance at runtime but still confused about

  1. What is precompiled SQL Statement? What it does?
  2. Why precompiled statement is faster than usual SQL Statement?

Solution

  • The following steps are used by any database while running an SQL query:

    1. Convert given SQL query into DB format
    2. Check for syntax
    3. Check for semantics
    4. Prepare execution plan
    5. Set the run-time values into the query
    6. Run the query and fetch the output

    In case of Statement interface all above 6 steps need to be followed for each query execution. However, in case of PreparedStatement interface steps 1 to 4 will be done only once and steps 5 & 6 will be repeated for each execution of the query. This makes the PreparedStatement faster.

    Hope this answers your question...