aspose file tools*
The moose likes JDBC and the fly likes Diff b/w Statement and PreparedStatement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Diff b/w Statement and PreparedStatement" Watch "Diff b/w Statement and PreparedStatement" New topic
Author

Diff b/w Statement and PreparedStatement

Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
Can anyone tell me the difference between Statement and PreparedStatement?
What are the advantages of one over other and how are they executed


Thanks,
Shekar
Sreedhar mar
Ranch Hand

Joined: Sep 15, 2003
Posts: 34
Statement Executes static querys
PreparedStatement executes dynamic querys.These are pre-compiled and pass
arguments to the query.PreparedStatements are fast compare to Statement
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
PreparedStatements CAN be faster than Statements, but there is no guarantee of it. If the database doesn't cache the PreparedStatement then it will get re-compiled every time, leading to comparable execution time between the two. However when the database DOES cache them, you get a nice performance gain.
The other big advantage of using PreparedStatements is the ability to parameterize the values by the use of "?". You don't have to worry about whether your database uses single or double quotes to delimit strings, or how to format date values. You simply use "?" in the PreparedStatement and then to "setString()" or "setDate()" to set the parameter and the underlying vendor classes handle everything for you. It makes the code much more portable (database independance).
Chandra Bairi
Ranch Hand

Joined: Sep 12, 2003
Posts: 152
If the database does not cache the prepared statement then is the time taken to execute the Statement and PreparedStatement equal?
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
There are a lot of factors that can affect the time: network traffic, what database you are running, database indexes, etc. The best thing for you to do is write a simple app that inserts several thousand records into the database using prepared statements, and several thousand using 'regular' statements. See how long each set takes. The difference may be negligable, or it may be irrelevant considering what else your real application does.
The performance considerations aside, there is still great value in using PreparedStatements for the ease with which you can work with String and Date values, not to mention BLOB/CLOB objects or database-specific objects. Go with PreparedStatements.
Anurag Gupta
Ranch Hand

Joined: Dec 13, 2001
Posts: 40
As Wayne rightfully said, it depends upon the Datbase and Driver implementation mainly.
You could look for some additional info here.


Anurag Gupta
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Anarug .. that's a great link you provided. Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Diff b/w Statement and PreparedStatement