• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regarding preparedstatement

 
yekkala krishna
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

Shall we excuete select query with help of preparedstatement interface.

For example : select * from emp;

with regards
Y.rama krishna
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, because if you execute the same statement multiple times and you use PreparedStatement, the JDBC driver or the database can cache the statement so that it doesn't need to completely re-interpret the SQL, which will be more efficient.

(Ofcourse that will only work if you re-use the same PreparedStatement object, not if you create a new PreparedStatement each time you execute the same SQL statement).
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you can use this query with PreparedStatement. But that does NOT really save the purpose.

Because, the query seems to be fetching the same results always, it can be achieved by Statement itself.

Remember a famous sentence by K & B in their book (which i like), "Just because its allowed, does not mean that its right".

I dont mean to say that you CANNOT run this through PreparedStatement but whats the real or actual use of it? If at all your query seems to have a variable input at runtime say you append the department id like


Select * from emp where dept_id = ?


it makes sense to use PreparedStatement as at runtime you can reuse the same query by passing different department id values.

HtH.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic