数据库 JDBC

preparedStatement和statement的区别与联系:

在JDBC应用中,如果你已经是稍有水平开发者,你就应该始终以PreparedStatement代替Statement.也就是说,在任何时候都不要使用Statement。 

Statement为一条Sql语句生成执行计划, 如果要执行两条sql语句,select colume from table where colume=1;select colume from table where colume=2; 会生成两个执行计划 一千个查询就生成一千个执行计划! PreparedStatement用于使用绑定变量重用执行计划 select colume from table where colume=:x; 通过set不同数据只需要生成一次执行计划,可以重用

两者联系

  • PreparedStatement 继承 Statement ,   PreparedStatement 实例包含已编译的 SQL 语句, 所以其执行速度要快于 Statement 对象。
  • 作为 Statement 的子类,PreparedStatement 继承了 Statement 的所有功能。三种方法execute、 executeQuery 和 executeUpdate 已被更改以使之不再需要参数
  • PreparedStatement尽最大可能提高性能. 最重要的一点是极大地提高了安全性.

猜你喜欢

转载自www.cnblogs.com/lucky1024/p/11042777.html