博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Statement VS PrepareStatement

Posted on 2014-03-12 20:40  钟悍  阅读(281)  评论(0编辑  收藏  举报

Many relation database handle a JDBC sql query in blew four steps:

1, parse the incoming sql

2, compile the sql

3, plan/ optimize the sql.

4, execute the sql and return data

A Statement will always procceed the four steps for each sql quer sent to the database. A PrepareStatement will pre-execute the steps 1-3.

Thus, when creating a PreparedStatement some pre-optimization is performed immediately. 

 

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: 

preparedStatement also will avoid some sql injection risk.

 

callablestatement extend the preparestatement, it can excute a procedure.