JDBC之Statement、PreparedStatement和CallableStatement
JDBC提供了Statement、PreparedStatement和CallableStatement三种方式来执行查询语句,其中Statement用于通用查询,PreparedStatement用于执行参数化查询,而CallableStatement则是用于存储过程
1. Statement、PreparedStatement和CallableStatement都是接口(interface)。
2. Statemet继承自Wrapper、PreparedStatement继承自Statement、CallableStatement继承自PrepareStatement。
3. Statement接口提供了执行语句和获取结果的基本方法;
PreparedStatement接口添加了处理IN参数的方法;
CallableStatement接口添加了处理OUT参数的方法。
4. Statement接口:普通的不带参的查询SQL;每次执行SQL语句时,数据库都要编译该SQL语句;支持批量更新,批量删除。
PreparedStatement接口:可变参数的SQL,编译一次,执行多次,效率高;安全性好,有效防止SQL注入等问题;支持批量更新,批量删除。
CallableStatement接口:继承自PreparedStatement,支持带参数的SQL操作;支持调用存储过程,提供了对输出和输入(IN、OUT)参数的支持。
Statement每次执行SQL语句,数据库都要执行SQL语句的编译,最好用于仅执行一次查询并返回结果的情形,效率高于PreparedStatement。
PreparedStatement是预编译的,使用它的几个好处:
1. 在执行可变参数的一条SQL时,PreparedStatement比Statement的效率高,因为DBMS预编译一条SQL当然会比好多次编译SQL的效率要高。
2. 安全性好,有效防止SQL注入等问题。
3. 对于多次重复执行的语句,使用PreparedStatement效率会更高一点,并且在这种情况下也比较适合使用batch。
4. 代码的可读性和可维护性。
JDBC之Statement、PreparedStatement和CallableStatement的更多相关文章
- Statement, PreparedStatement和CallableStatement的区别
Statement用于执行不带参数的简单SQL语句,并返回它所生成的结果,每次执行SQL豫剧时,数据库都要编译该SQL语句. Satatement stmt = conn.getStatement() ...
- 执行对象Statement、PreparedStatement和CallableStatement详解 JDBC简介(五)
执行对象是SQL的执行者,SQL是“安排好的任务”,执行对象就是“实际工作的人”. 执行对象有三种: Statement.PreparedStatement和CallableStatement,他们都 ...
- Statement、PreparedStatement、CallableStatement的区别
此三个接口的声明如下: public interface Statement extends Wrapper, AutoCloseable public interface PreparedState ...
- java基础 JDBC & Statement & PreparedStatement
参考文章: http://blog.csdn.net/wang379275614/article/details/23393335 概念 JDBC-数据库连接,是由一些类和接口构成的API,是J2SE ...
- 执行对象Statement、PreparedStatement和CallableStatement详解
执行对象是SQL的执行者,SQL是"安排好的任务",执行对象就是"实际工作的人". 执行对象有三种: Statement.PreparedStatement和C ...
- 说说Statement、PreparedStatement和CallableStatement的异同(转)
1.Statement.PreparedStatement和CallableStatement都是接口(interface). 2.Statement继承自Wrapper.PreparedStatem ...
- Statement、 PreparedStatement 、CallableStatement 区别和联系
Statement. PreparedStatement .CallableStatement 区别和联系 1. Statement.PreparedStatement和CallableStateme ...
- JDBC的Statement对象
以下内容引用自http://wiki.jikexueyuan.com/project/jdbc/statements.html: 一旦获得了数据库的连接,就可以和数据库进行交互.JDBC的Statem ...
- jdbc执行Statement接口的步骤
jdbc执行Statement接口的步骤如下: 1)驱动注册程序: Class.forName(com.mysql.jdbc.Driver); 2)获取连接对象: Connection conn = ...
随机推荐
- 【leetcode】1030. Matrix Cells in Distance Order
题目如下: We are given a matrix with R rows and C columns has cells with integer coordinates (r, c), whe ...
- 按照MySQL
转载自:https://mp.weixin.qq.com/s?__biz=MzIwNzk0NjE1MQ==&mid=2247484200&idx=1&sn=6eed12242c ...
- NoSQL数据库的分布式算法详解
系统的可扩展性是推动NoSQL运动发展的的主要理由,包含了分布式系统协调,故障转移,资源管理和许多其他特性.这么讲使得NoSQL听起来像是一个大筐,什么都能塞进去.尽管NoSQL运动并没有给分布式数据 ...
- Python_013(面向对象概念)
一.面向对象 1.面向对象几个概念问题: a:类:具有相同属性和技能的一类事物.用代码表示就是,我类里面有一些静态变量和方法是大家共有的; b:对象:具体的类的表现.在代码中就是,调用类的方法或变量传 ...
- Strange fuction hdu 2899
Strange fuction Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- 动态规划 List
例题 #A 传纸条(Accepted) #B 乘积最大 (Unaccepted) #C 石子合并 (Accepted) #D 加分二叉树 (Unaccepted) #E 没有上 ...
- ANTLR4的IntelliJ IDEA配置
1.配置的先导工作 jdk配置 IntelliJ IDEA安装配置 配置好ANTLR的java运行时环境,参考 2.下载intellij-plugin-v4.zip 下载地址 3.插件安装 4.测试安 ...
- Python基础学习:operator模块
operator——函数的标准操作 转自:https://blog.csdn.net/zhtysw/article/details/80510113 代码资源:Lib/operator.py oper ...
- day09—css布局解决方案之全屏布局
转行学开发,代码100天——2018-03-25 今天,本文记录全屏布局的的方法.全屏布局,即滚动条不是全局滚动条,而是出现在内容区域内,:浏览器变大时,撑满窗口. 如:设置下图中布局,其中top区, ...
- 用BP人工神经网络识别手写数字
http://wenku.baidu.com/link?url=HQ-5tZCXBQ3uwPZQECHkMCtursKIpglboBHq416N-q2WZupkNNH3Gv4vtEHyPULezDb5 ...