• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Diff b/w Statement and PreparedStatement

 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anarug .. that's a great link you provided. Thanks!
 
Dinner will be steamed monkey heads with a side of tiny ads.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic