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 Differnce between Statement & 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 "Differnce between Statement & PreparedStatement" Watch "Differnce between Statement & PreparedStatement" New topic
Author

Differnce between Statement & PreparedStatement

Shrinivas Mujumdar
Ranch Hand

Joined: Aug 27, 2004
Posts: 328
Hello Friends,
I want to know the difference between Statement & PreparedStatement.Is there any performance boost in using PreparedStatemnt for STATIC SQL Queries (e.g. SELECT * FROM EMP)compared to Statement?

Thanks in Advance,
Shriniwas
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

The use of a Statement in JDBC should be 100% localized to being used for DDL (ALTER, CREATE, GRANT, etc) as these are the only statement types that cannot accept BIND VARIABLES.

PreparedStatements or CallableStatements should be used for EVERY OTHER type of statement (DML, Queries). As these are the statement types that accept bind variables.

This is a fact, a rule, a law -- use prepared statements EVERYWHERE. Use
STATEMENTS almost no where.


Also read http://faq.javaranch.com/view?PreparedStatement


Shailesh


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30758
    
156

Originally posted by Shrinivas Mujumdar:
Is there any performance boost in using PreparedStatemnt for STATIC SQL Queries (e.g. SELECT * FROM EMP)compared to Statement?

Yes. The PreparedStatement still gets cached.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Differnce between Statement & PreparedStatement