Sql – Statement vs Prepared Statement in terms of Precompilation

jdbcsql

I understand the main difference between using Statement and PreparedStatement (PreparedStatements allow passing in parameters). But I read about a subtle difference between the two – namely that PreparedStatements can be quicker than generic Statements, because PreparedStatement SQL is precompiled.

What exactly does being precompiled mean, and why does it make a difference?

Best Answer

A prepared statement performs the following checks:

  • Makes sure that the tables and columns exist
  • Makes sure that the parameter types match their columns
  • Parses the SQL to make sure that the syntax is correct
  • Compiles and caches the compiled SQL so it can be re-executed without repeating these steps