• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Diff b/w Statement and PreparedStatement

 
Chandra Bairi
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me the difference between Statement and PreparedStatement?
What are the advantages of one over other and how are they executed
 
Sreedhar mar
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Wayne rightfully said, it depends upon the Datbase and Driver implementation mainly.
You could look for some additional info here.
 
Wayne L Johnson
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anarug .. that's a great link you provided. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic