Difference between Statement and PreparedStatement?


Answer posted by pravash on 2005-06-08 02:16:34: 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 parametrizednull 
 
Most relational databases handles a JDBC / SQL query in four steps:  
1.Parse the incoming SQL query  
2. Compile the SQL query  
3. Plan/optimize the data acquisition path  
4. 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.  
 
Another advantage of the PreparedStatement class is the ability to create an incomplete query and supply parameter values at execution time. This type of query is well suited for filtering queries which may differ in parameter value only:  
 
 
SELECT firstName FROM employees WHERE salary > 50 
SELECT firstName FROM employees WHERE salary > 200  
 
 
 
 
This question is related to Oracle Interview

Showing Answers 1 - 1 of 1 Answers

pravash

  • Jun 8th, 2005
 

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 parametrizednull 
 
Most relational databases handles a JDBC / SQL query in four steps:  
1.Parse the incoming SQL query  
2. Compile the SQL query  
3. Plan/optimize the data acquisition path  
4. 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.  
 
Another advantage of the PreparedStatement class is the ability to create an incomplete query and supply parameter values at execution time. This type of query is well suited for filtering queries which may differ in parameter value only:  
 
 
SELECT firstName FROM employees WHERE salary > 50 
SELECT firstName FROM employees WHERE salary > 200  
 
 
 
 

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions