jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases 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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "regarding preparedstatement" Watch "regarding preparedstatement" New topic

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: 15101

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: 3381

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.


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
jQuery in Action, 3rd edition