• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Prepared Statement and Statement

 
Jerry Ragland
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried searching for the below questions in the Ranch but couldn't find what I want. Excuse me if the below questions where already answered and please point me to the thread if so..

1. Difference between a PreparedStatement and Statement
2. When I should go for a PreparedStatement and when for Statement?
3. Which is better of the two in performance?
4. I heard that PreparedStatement and Statement take different no. of interations when used in a loop - Is that correct?

Thanks in advance,
Jerry.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. Difference between a PreparedStatement and Statement

This is best answered by looking at the JavaDocs


2. When I should go for a PreparedStatement and when for Statement?

There is not absolute rule, but a general rule of thumb could be to use PreparedStatments for the vast majority of operations with the possible exception of running adhoc queries that change frequently. PreparedStatements hide a lot of the issues surrounding formatting and escaping characters in SQL queries etc. They are also compiled and added to a cache, so should perform better over time.


3. Which is better of the two in performance?

See above. Of course query performance is a much bigger topic than a simple choice of whether to use Statements or PreparedStatements.



4. I heard that PreparedStatement and Statement take different no. of interations when used in a loop - Is that correct?

Where did you hear that? What makes you think this would be the case?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic