This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executing PreparedStatement only once and Executing Statement only once

 
Karthik Prabu
Ranch Hand
Posts: 48
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Ram Kumar Subramaniam
Ranch Hand
Posts: 68
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic