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