wood burning stoves*
The moose likes JDBC and the fly likes Difference between JDBC Statement and PreparedStatement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "Difference between JDBC Statement and PreparedStatement " Watch "Difference between JDBC Statement and PreparedStatement " New topic
Author

Difference between JDBC Statement and PreparedStatement

Anand Jain
Greenhorn

Joined: Jul 27, 2009
Posts: 18
What is the difference between Statement and PreparedStatement in JDBC ? Explain it with some example.
When to use either of these?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

The best place to answer this sort of question are the JavaDocs.

To paraphrase what you will find there, Statements are used to execute adhoc SQL, PreparedStatements represent precompiles SQL and include parameter binding.


When to use either of these?

Statements: almost never. They are useful when you are running user generated SQL i.e. if you are writing a SQL client, or when you are runnign SQL that has no user input.

PreparedStatements: most of the time.





JavaRanch FAQ HowToAskQuestionsOnJavaRanch
mark goking
Ranch Hand

Joined: Aug 18, 2009
Posts: 155
preparedstatements are also a good way if you plan to execute the same sql statements more than once but with different values. you can use the same preparedstatement object whilst changing their values for every sql statement you execute


Website/Java Games: http://www.chitgoks.com
Tech Blog: http://tech.chitgoks.com
 
GeeCON Prague 2014
 
subject: Difference between JDBC Statement and PreparedStatement