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 Why PreparedStatement is so called precompiled? 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 "Why PreparedStatement is so called precompiled? " Watch "Why PreparedStatement is so called precompiled? " New topic
Author

Why PreparedStatement is so called precompiled?

Anil Sharma
Greenhorn

Joined: Nov 07, 2005
Posts: 15
Hi All,

Can anybody explain me
'Why PreparedStatement is so called precompiled?'

Is it compiled already in Database Driver or somewhere else?
Why
"Statement is not so called precompiled?"


what is the main difference between them?

please clear my doubts.

Thanks in Advance


Anil Sharma<br />SCJP 1.4, SCWCD 1.4(Preparing)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

With a PreparedStatement, the database driver has a chance to look at the statement and optimize it just once, and then you can reuse that work multiple times. That's because with a PreparedStatement, the SQL is specified in the constructor. With a regular Statement, the SQL you pass to the executeQuery() method is only executed once, so the driver has to redo that optimization every time you do a query. This means PreparedStatements can be a lot more efficient.


[Jess in Action][AskingGoodQuestions]
Rajesh Agarwal
Ranch Hand

Joined: Aug 01, 2005
Posts: 79
Anil,

With every query that reaches the database engine, it actually creates an execution plan based on which the records are fetched. In a PreparedStatement, all this work is done before hand and the plans need not be recreated, so with a PS the plan is already in place and this makes the execution faster. But with Statement this is prepared everytime, and thereby the execution is a bit slower as compared to the PS.
Anil Sharma
Greenhorn

Joined: Nov 07, 2005
Posts: 15
thanks a lot friends
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why PreparedStatement is so called precompiled?