• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Executing PreparedStatement only once and Executing Statement only once

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
Will there be any difference in execuitng a "PreparedStatement" only once and Executing an "Statement" Only once ? In my program I have to construct a query with many runtime parameters, but i need to execute the query only once. If I use "PreparedStatement" it will be easy to maintain the parameters by avoiding constructing the dynamic sql. Please comment if this approach will introduce any Performance issues as I am using "PreparedStatement".
Thanks in advance
Prabu
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are some (very few) occasions where using a statement will be faster than using a PreparedStatement, but since you're unlikely to be able to tell the difference in the performance of your code, I wouldn't worry about it.
I tend to almost always default to PreparedStatements since we almost always use some type of persistence mechanism (castor is our favourite flavour at the moment) and these tend to use PreparedStatements under the covers.
Dave
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a thought !!! Not about performance ....
if you were using Statements then sometimes u might run into String related problems ..... for example consider the string --ram's car-- . Ok so that single quotes will give u problems.
Best way to handle those would be to just use prepared statements ... use pm.setString(1,urString)
 
Karthik Prabu
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David

There are some (very few) occasions where using a statement will be faster than using a PreparedStatement.


Can u tell me on what occasions Statement will be faster than PreparedStatement. It will be really helpful to me.
Thanks
Prabu
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If (as you say) you are only executing a query once, then consider the following to statements:

When you send them off to the database, the first thing it has to do before executing them is compile them. This should take the same amount of time for each query.
If you execute them both once, the statement should be slightly faster since the database can make more use of optimising tricks when it complies the query. The prepared statement version is less specific and therefore less optimised.
If you then run both queries again with the value 2000, the statement needs to be recompiled, but the query from the prepared statement shouldn't have to be. From this point on, the prepared staement would probably be the best bet.
I keep using terms like 'should be' and 'probably' since this is all very database dependent. If the database doesn't support PreparedStatements and support is just faked by the driver, you'll probably just get the same performance.
Dave
reply
    Bookmark Topic Watch Topic
  • New Topic