Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PreparedStatement vs. CreateStatement

 
Frank Hale
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could somebody illustrate the reasons I would use a PreparedStatement over a CreateStatement, I'm not quite sure which ones I should be using at times?
Thanks,
Frank
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Frank Hale:
Could somebody illustrate the reasons I would use a PreparedStatement over a CreateStatement, I'm not quite sure which ones I should be using at times?
Thanks,
Frank

createStatement() creates a Statement Object based on a fully qualified SQL String without parameters.
prepareStatement() creates a PreparedStatement Object out of a parameterized SQL String.
The use of prepareState has some additional overhead in the database the first time it is run. So, if you only need to run the statement once or twice for your application, createStatement is the best way to handle it. Addtionally if the components of your where clause are dynamic (ie you may add an additional qualifier to the where clause based on user input) you need to use the create statement.
However since the PreparedStatement Object gets compiled in the database, it will provide you with better performance if you need to repeatedly call the SQL and only change the values in the where clause.
Note: if you are always calling the same SQL you'll get the best performance converting it into an SQL Stored Procedure and calling it using the CallableStatement Object.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic