This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes PreparedStatement storage & lifetime Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
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
subject: PreparedStatement storage & lifetime
Similar Threads
JDBC preparedStatement vs Stored Proc ?
Problem passing PreparedStatement
how to distinguish the stack and the heap
Prepared Statement
How to determine Primary Key of New Record