mausam
Posts: 243
Nickname: mausam
Registered: Sep, 2003
|
|
Re: JDBC
|
Posted: Mar 23, 2004 11:11 PM
|
|
Statement prepares and executes the query plan each time, while PreparedStatement prepares the query plan once and then reuses the query plan. Preparing a statement is also referred to as precompiling a statement. If that were the whole story, then PreparedStatement would always be the statement of choice, and youwou ld avoid Statement objects completely. But itÂ’s not quite the whole story. Statement has optimizations that the database can apply; mainly, the database knows that the Statement plan is executed immediately and thrown away. So the database handles Statement queries differently from PreparedStatements. Statement queries can be prepared and executed in one swoop, using the state of the database at the time, without allocating resources to keeping the plan around. PreparedStatements, on the other hand, need to allocate database resources to store and maintain the query plan and to ensure that the query plan is not invalidated by changes to the database. For example, the query plan would need to be updated or re-created in the case of some types of changes to the database, depending on how detailed the query plan is.
These are the two links you should go
http://otn.oracle.com/oramag/books/oreilly/ch16_jdbc_shirazi.pdf http://www.jguru.com/faq/view.jsp?EID=693
Short answer:
The PreparedStatement is a slightly more powerful version of a Statement, and should always be at least as quick and easy to handle as a Statement. The PreparedStatement may be parametrized. Longer answer: Most relational databases handles a JDBC / SQL query in four steps:
Parse the incoming SQL query Compile the SQL query Plan/optimize the data acquisition path Execute the optimized query / acquire and return data
A Statement will always proceed through the four steps above for each SQL query sent to the database. A PreparedStatement pre-executes steps (1) - (3) in the execution process above. Thus, when creating a PreparedStatement some pre-optimization is performed immediately. The effect is to lessen the load on the database engine at execution time.
|
|