On the DataBase. If you have a trace utitlity with whichever DataBase you currently use you can turn it on and watch what happens. The first time a PreparedStatement is called you probably see the actual SQL it is compiled from. Every subsequent time you are more likely to see "execute statment " statements.
Originally posted by Adeel Ansari: Statement is compiled by the driver and stored in PreparedStatement object.
Are you sure? My understanding is the expensive part of a running a statement against a DB is that the DB's statement engine first need to interpret the SQL, then calculate an efficient query plan, and that's what gets compiled. So the driver itself doesn't do the compilation, it the DB. And true a version of the statment is held in a PreparedStatment object, but that is just used as a key to the actual compiled PreparedStatement held in the statement cache in the DB?
Prepared SQL statements get compiled in the database only once, future invocations do not recompile them
Ah right - so we are describing the same process. I understood your previous post as "the statement is always compiled by the driver and only stored in the PreparedStatament object", which reading your other posts seems to not be what you meant.
I read it the same way. Funny when two people disagree with the same answer
Two extra points on PreparedStatements:
1) typically the PS is 'matched' against the SQL String, so you should ensure the String you contruct the PS with is always the same. I believe that in Oracle it is even case-sensitive. 2) In some databases that don't support query caching, support is faked in the JDBC Driver. I think MySQL is an example of this.