This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes PreparedStatement vs. CreateStatement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "PreparedStatement vs. CreateStatement" Watch "PreparedStatement vs. CreateStatement" New topic
Author

PreparedStatement vs. CreateStatement

Frank Hale
Ranch Hand

Joined: Mar 25, 2000
Posts: 230
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

Joined: Jun 13, 2000
Posts: 3340
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.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PreparedStatement vs. CreateStatement
 
Similar Threads
Making ResultSet object scrollable
How to rollback multiple prepared statements
What exactly we mean by Precompiled SQL Statement
Regarding createStatement();
very strange