tags:

views:

261

answers:

1

We have a Prepared Statement in Java and we hear that it is different from Statement in the Way that this prepared statement need not be compiled every time it is used.

My question is where is this Compiled Statement stored ? In the Client code that uses it or is it stored by the Database ?

Why would a DB Store a Compiled statement and if it does than for how long ?

+3  A: 

Databases have a query cache, which means that when you execute a query the database caches the parsed/compiled query and maybe the query plan to eliminate future computations.

The prepared statements are typically cached first at the application level, where the application container is responsible for managing the statements cache. Most application containers have options to control the prepared statements cache (eg: glassfish).

As you can see here, the main difference is like you stated: with prepared statements, the application reduce the cost of compiling the same statement over and over. As a side note, for most app containers the statement must be exactly the same (whitespace included) in order to be reused correctly, so be careful when using hand-written queries.

Miguel Ping
Mig,That means the JDBC Driver has the capability to convert a Prepared Statement to a Compiled Statement ? Which means the Driver is smart enough to parse a query and identify Syntactical mistakes ?
Geek
AFAIK, the driver doesn't parse queries, only the DB does that. Every syntatic error is thrown by the DB parser. But the app server is smart enough to reuse same statements where only the args (placeholders '?') differ.
Miguel Ping