File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes PreparedStatement storage & lifetime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "PreparedStatement storage & lifetime" Watch "PreparedStatement storage & lifetime" New topic

PreparedStatement storage & lifetime

Matt Dole

Joined: Mar 06, 2001
Posts: 17
i understand that a PreparedStatement is precompiled but where is it stored and what is its lifetime?
Is it stored by the database and the database manages its lifetime? Thus the db maintains a pool of PreparedStatement objects that it refers to?
Is it a regular java object thus obeys the standard java object lifetime rules? If so, would there be any benefit to using a PreparedStatement object instead of a Statement object in a method that created a method local PreparedStatement object and used it once?
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

PreparedStatements (if supported) are completely owned and controlled by the database. The PreparedStatement class in JDBC is just to give you access to it.
On a side note, MySQL doesn't have any concept of PreparedStatements and in this case they are managed by the Driver.
Amy Smith

Joined: Jul 26, 2001
Posts: 24
We are using PreparedStatements from a Solaris web server with the database on DB2V5/OS390.
Your database analyst could probably answer your questions about how long it exists on the database.
From the java side, I believe the PreparedStatement closes when the Connection Object is closed if you don't close it earlier. If it stays around on the database, I do not know how to re-use it from JDBC. That doesn't mean you can't though.
We use PreparedStatement for all queries because each query is stored in it's own class and it allows us to parameterize them for re-use. A regular Statement does not allow parameters.
(Is parameterize a real word? :roll: )

Amy Smith<br />Java Developer<br />Haworth, Inc.<br />
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Sorry, I forgot that I also had this link to an article on PreparedStatements at TheServerSide
I agree. Here's the link:
subject: PreparedStatement storage & lifetime
It's not a secret anymore!