GeeCON Prague 2014*
The moose likes JDBC and the fly likes Executing PreparedStatement only once and Executing Statement only once Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Executing PreparedStatement only once and Executing Statement only once" Watch "Executing PreparedStatement only once and Executing Statement only once" New topic
Author

Executing PreparedStatement only once and Executing Statement only once

Karthik Prabu
Ranch Hand

Joined: Dec 27, 2002
Posts: 48
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jan 17, 2003
Posts: 68
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

Joined: Dec 27, 2002
Posts: 48
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

Joined: Mar 06, 2001
Posts: 13459

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Executing PreparedStatement only once and Executing Statement only once