PreparedStatement与Statement的区别和联系
2013-04-03 15:38
204 查看
1、关系
PreparedStatement继承Statement,他们属于父与子的关系。使用上来说Statement使用的地方都可以换成PreparedStatement。
2、安全性
Statement的直接执行SQL语句,无法防止SQL注入问题。PreparedStatement可以使用占位符,可以防止SQL注入问题。
这里举例说明:
使用Statement
stmt.executeQuery("select * from users where lastname = '"+lastName+"'");
使用PreparedStatement
perstmt = con.prepareStatement("select * from users where lastname = ?");
perstmt.setString(1,lastName);
rs=perstmt.executeQuery();
从上面可以明显看出使用Statement时,如果对lastName的值进行恶意的修改,那么就可能出现进行额外的操作、类型等等错误。最简单的“' or 1 or'”或关键字等等。而PreparedStatement由于内置了字符过滤,那么就相当于 where name = ' or 1 or'显然没有对应记录.
这就体现了PreparedStatement的防注入功能,将一些常用的错误屏蔽掉。
3、性能
PreparedStatement:
1) addBatch()将一组参数添加到PreparedStatement对象内部。
2) executeBatch()将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。
Statement:
1) addBatch(String sql)方法会在批处理缓存中加入一条sql语句。
2) executeBatch()执行批处理缓存中的所有sql语句。
从两者的executeBatch方法的使用说明我们就可以明显的看出:
从第一次执行的角度:PreparedStatement由于占位符的使用,第一次执行需要很多额外的工作,例如占位符转换等等,这些都需要消耗一定的事件和资源。而Statement直接执行相对来说效率要高、消耗要少一些。
从多次执行的角度:PreparedStatement由于使用占位符进行预编译时,保存的执行代码被缓存,下次调用的时候就可以不再被编译而可以直接执行。而Statement即使执行一样的操作,由于操作数据不同,所以执行语句也需要再次编译。
PreparedStatement继承Statement,他们属于父与子的关系。使用上来说Statement使用的地方都可以换成PreparedStatement。
2、安全性
Statement的直接执行SQL语句,无法防止SQL注入问题。PreparedStatement可以使用占位符,可以防止SQL注入问题。
这里举例说明:
使用Statement
stmt.executeQuery("select * from users where lastname = '"+lastName+"'");
使用PreparedStatement
perstmt = con.prepareStatement("select * from users where lastname = ?");
perstmt.setString(1,lastName);
rs=perstmt.executeQuery();
从上面可以明显看出使用Statement时,如果对lastName的值进行恶意的修改,那么就可能出现进行额外的操作、类型等等错误。最简单的“' or 1 or'”或关键字等等。而PreparedStatement由于内置了字符过滤,那么就相当于 where name = ' or 1 or'显然没有对应记录.
这就体现了PreparedStatement的防注入功能,将一些常用的错误屏蔽掉。
3、性能
PreparedStatement:
1) addBatch()将一组参数添加到PreparedStatement对象内部。
2) executeBatch()将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。
Statement:
1) addBatch(String sql)方法会在批处理缓存中加入一条sql语句。
2) executeBatch()执行批处理缓存中的所有sql语句。
从两者的executeBatch方法的使用说明我们就可以明显的看出:
从第一次执行的角度:PreparedStatement由于占位符的使用,第一次执行需要很多额外的工作,例如占位符转换等等,这些都需要消耗一定的事件和资源。而Statement直接执行相对来说效率要高、消耗要少一些。
从多次执行的角度:PreparedStatement由于使用占位符进行预编译时,保存的执行代码被缓存,下次调用的时候就可以不再被编译而可以直接执行。而Statement即使执行一样的操作,由于操作数据不同,所以执行语句也需要再次编译。
相关文章推荐
- Statement,PreparedStatement和CallableStatement的联系和区别
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- PreparedStatement与Statement的区别和联系
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- JDBC:Statement与PreparedStatement的联系与区别
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- preparedStatement和statement,CallableStatemen的区别与联系
- Statement,PreparedStatement,CallableStatement 的联系和区别
- preparedstatement与statement之间的区别与联系
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- NFV与SDN的区别与联系
- Linux基础:信号量和互斥锁区别与联系
- CountDownLatch类与join函数的 使用、区别与联系
- ExtJS 是什么?与JavaScript、Ajax和Ext的区别及联系是什么?
- Delphi New,Getmem,ReallocMem联系与区别
- C++ STL,ATL,WTL之间的联系和区别
- ASCII、Unicode、GBK和UTF-8字符编码的区别联系