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 Statement vs. Prepared Statement 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 "Statement vs. Prepared Statement" Watch "Statement vs. Prepared Statement" New topic

Statement vs. Prepared Statement

Debalina Bera

Joined: Jun 08, 2003
Posts: 5
I want to know the clear difference between the use of statement and prepared statement. In case of prepared statement, the object is created first time in the memory of database and can be used for all later invocation. If that is true, in all our web application, we should use prepared statement always, since multiple users will be accessing the website and the same object of that prepared statement can be used multiple times. My doubt is that, what is the purpose of having normal statement then? Another confusion, when at the end of method we close the prepared statement, does the object get closed from the memory of the database as well or only gets closed from the server?
Appreciate your detailed input on this.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
First of all, you should read this article.
Second, you don't have to worry about any "objects" being "closed" in the database. What the use of PreparedStatement does in the database is basically caching; the database engine compiles the SQL skeleton into a query plan and caches the resulting "executable". It's up to the database server whether it wants to keep the compiled stuff in memory after the statements have been executed and committed.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
I agree. Here's the link:
subject: Statement vs. Prepared Statement
It's not a secret anymore!