File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes regarding preparedstatement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "regarding preparedstatement" Watch "regarding preparedstatement" New topic
Author

regarding preparedstatement

yekkala krishna
Ranch Hand

Joined: Mar 04, 2007
Posts: 105
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

Joined: Aug 16, 2005
Posts: 14269
    
  21

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).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: regarding preparedstatement