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 Difference between JDBC Statement and PreparedStatement 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 "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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Difference between JDBC Statement and PreparedStatement
 
Similar Threads
PreparedStament and Statement
JDBC
Difference Between PreparedStatement and Statement and CollableStatement?
Difference between Statement/Prepared statement?
Prepared Statement