Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between JDBC Statement and PreparedStatement

 
Anand Jain
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between Statement and PreparedStatement in JDBC ? Explain it with some example.
When to use either of these?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.




 
mark goking
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic