JDBC中Statement、PreparedStatement 、CallableStatement 区别和联系
来源:互联网 发布:js脚本怎么写 编辑:程序博客网 时间:2024/06/11 20:59
Statement、PreparedStatement 、CallableStatement 区别和联系
1. Statement、PreparedStatement和CallableStatement都是接口(interface)。
2. Statement继承自Wrapper、PreparedStatement继承自Statement、CallableStatement继承自PreparedStatement。
3. Statement接口提供了执行语句和获取结果的基本方法;
PreparedStatement接口添加了处理 IN 参数的方法;
CallableStatement接口添加了处理 OUT 参数的方法。
4. a. Statement:
普通的不带参的查询SQL;支持批量更新,批量删除;
b. PreparedStatement:
可变参数的SQL,编译一次,执行多次,效率高;
安全性好,有效防止Sql注入等问题;
支持批量更新,批量删除;
c. CallableStatement:
继承自PreparedStatement,支持带参数的SQL操作;
支持调用存储过程,提供了对输出和输入/输出参数(INOUT)的支持;
Statement每次执行sql语句,数据库都要执行sql语句的编译 ,最好用于仅执行一次查询并返回结果的情形时,效率高于PreparedStatement。
PreparedStatement的好处:
PreparedStatement是预编译的,使用PreparedStatement有几个好处。
1. 在执行可变参数的一条SQL时,PreparedStatement比Statement的效率高,因为DBMS预编译一条SQL当然会比多次编译一条SQL的效率要高。
2. 安全性好,有效防止Sql注入等问题。
3. 对于多次重复执行的语句,使用PreparedStament效率会更高一点,并且在这种情况下也比较适合使用batch;
4. 代码的可读性和可维护性。
注:
executeQuery:执行给定的SQL 语句,该语句返回单个 ResultSet 对象。这个方法被用来执行 SELECT 语句,它几乎是使用最多的 SQL 语句。
executeUpdate: 用于执行INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。INSERT、UPDATE或 DELETE 语句的效果是修改表中零行或多行中的一列或多列。executeUpdate 的返回值是一个整数,指示受影响的行数(即更新计数)。对于CREATE TABLE 或 DROP TABLE 等不操作行的语句,executeUpdate 的返回值总为零。返回值为-1代表更新不成功。
execute: 用于执行返回多个结果集、多个更新计数或二者组合的语句。
- JDBC中Statement、PreparedStatement 、CallableStatement 区别和联系
- Statement,PreparedStatement,CallableStatement 的联系和区别
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- Statement、 PreparedStatement 、CallableStatement 区别和联系
- jdbc中Statement、PreparedStatement和CallableStatement的区别
- JDBC中Statement、PreparedStatement、CallableStatement的区别
- JDBC: Statement、PreparedStatement和CallableStatement
- JDBC--Statement、PreparedStatement和CallableStatement
- Statement、PreparedStatement和CallableStatement区别
- Statement、PreparedStatement和CallableStatement区别
- JDBC中Statement,PreparedStatement和CallableStatement的比较
- JDBC中Statement、PreparedStatement和CallableStatement的使用
- JDBC之Statement,PreparedStatement,CallableStatement的区别
- JDBC--Statement,PreparedStatement,CallableStatement的区别
- JDBC--Statement,PreparedStatement,CallableStatement的区别
- 机器学习面试
- Linux下单分支if语句下 unary operator expected错误解决方法
- 2016区域赛青岛赛区总结(2016/11/15)
- 微信小程序开发-自定义模态对话框实例
- Spring Cloud入门二:eureka集群
- JDBC中Statement、PreparedStatement 、CallableStatement 区别和联系
- HDU 6080 度度熊保护村庄(计算几何+floyd)
- java 静态方法和实例方法的区别
- Java 用反射将JavaBean转成Map
- java学习之Iterator(迭代器)的基础用法(转)
- (19)备份与恢复、导入导出
- PHP内置的字符串长度函数strlen mb_strlen
- Girls and Boys HDU
- UVA1641ASCIIArea