Statement、PreparedStatemnt、CallableStatement
第一、Statement(Statement代表一个特定的容器,来对一个特定的数据库执行语句)
* 执行查询的方法
Statement=Connection.createStatement();//创建执行sql的句柄对象
ResultSet=Statement.executeQuery(sql);//执行查询,返回结果集
* 执行增删改的方法
Statement=Connection.createStatement();//创建执行sql的句柄对象
int t=Statement.executeUpdate(sql);//执行插入,返回的是影响的行数
}
第二、PreparedStatemnt(PreparedStatemnt用于执行预编译的SQL语句。)
*执行预编译的查询方法
String sql="select * from student where sage>? and sname like ?";
PreparedStatemnt=Connection.prepareStatement(sql);//把sql语句发送到数据库中,执行预编译
PreparedStatemnt.setInt(1, 第一个?号的值);
PreparedStatemnt.setString(2, 第二个?号的值);
ResultSet=PreparedStatemnt.executeQuery();//执行查询,返回结果集
*执行预编译插入方法
String sql="insert into student values(?,?,?,?)";
PreparedStatemnt=Connection.prepareStatement(sql);//把预编译的sql语句发送到数据库中,执行预编译
PreparedStatemnt.setString(1, 第一个?的值);
PreparedStatemnt.setInt(2, 第二个?的值);
PreparedStatemnt.setByte(3, 第三个?的值);
PreparedStatemnt.setString(4, 第四个?的值);
int count=PreparedStatemnt.executeUpdate();//执行插入,返回的结果为影响的行数
第三、CallableStatement(CallableStatement用于执行对一个数据库内嵌过程的调用)
*调用有in参数的存储过程(查询)
CallableStatement=Connection.prepareCall("{call 存储过程名(?)}");//在应用程序里面调用数据库里的存储过程,? 分别对 应存储过程的参数
CallableStatement.setString(1, in参数的值);
ResultSet=CallableStatement.executeQuery();//执行查询,获得结果集
*调用有in参数的存储过程(插入)
CallableStatement=Connection.prepareCall("{call 存储过程名(?,?,?,?)}");//在应用程序里面调用数据库里的存储过程,? 分别对应存储过程的参数
CallableStatement.setString(1, 第一个in参数);
CallableStatement.setInt(2, 第二个in参数);
CallableStatement.setByte(3, 第三个in参数);
CallableStatement.setString(4, 第四个in参数);
int count=CallableStatement.executeUpdate();//执行插入,返回的结果为影响的行数
*调用有in、out参数的存储过程
CallableStatement=Connection.prepareCall("{call 存储过程名(?,?)}");//执行存储过程的查询方法
CallableStatement.setInt(1, ids);//输入参数
CallableStatement.registerOutParameter(2, Types.VARCHAR);//注册out参数
CallableStatement.execute();//执行查询
String name=CallableStatement.getString(2);//输出out参数
Statement、PreparedStatemnt、CallableStatement的更多相关文章
- 说说Statement、PreparedStatement和CallableStatement的异同(转)
1.Statement.PreparedStatement和CallableStatement都是接口(interface). 2.Statement继承自Wrapper.PreparedStatem ...
- 执行对象Statement、PreparedStatement和CallableStatement详解 JDBC简介(五)
执行对象是SQL的执行者,SQL是“安排好的任务”,执行对象就是“实际工作的人”. 执行对象有三种: Statement.PreparedStatement和CallableStatement,他们都 ...
- Statement、PreparedStatement、CallableStatement的区别
此三个接口的声明如下: public interface Statement extends Wrapper, AutoCloseable public interface PreparedState ...
- Statement、 PreparedStatement 、CallableStatement 区别和联系
Statement. PreparedStatement .CallableStatement 区别和联系 1. Statement.PreparedStatement和CallableStateme ...
- JDBC之Statement、PreparedStatement和CallableStatement
JDBC提供了Statement.PreparedStatement和CallableStatement三种方式来执行查询语句,其中Statement用于通用查询,PreparedStatement用 ...
- 执行对象Statement、PreparedStatement和CallableStatement详解
执行对象是SQL的执行者,SQL是"安排好的任务",执行对象就是"实际工作的人". 执行对象有三种: Statement.PreparedStatement和C ...
- java_JDBC,连接数据库方式,RestSet结果集,Statement,PreparedStatement,事务,批处理,数据库连接池(c3p0和Druid)、Apache-DBUtils、
一.JDBC的概述 1.JDBC为访问不同的数据薛是供了统一的接口,为使用者屏蔽了细节问题.2. Java程序员使用JDBC,可以连接任何提供了JDBC驱动程序的数据库系统,从而完成对数据库的各种操作 ...
- 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete
上篇文章<深入浅出Mybatis系列(六)---objectFactory.plugins.mappers简介与配置>简单地给mybatis的配置画上了一个句号.那么从本篇文章开始,将会介 ...
- MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存
二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...
随机推荐
- 快速排序原理及Java实现
1.基本思想: 快速排序是我们之前学习的冒泡排序的升级,他们都属于交换类排序,都是采用不断的比较和移动来实现排序的.快速排序是一种非常高效的排序算法,它的实现,增大了记录的比较和移动的距离,将关键字较 ...
- Python中的文件IO操作(读写文件、追加文件)
Python中文件的读写包含三个步骤:打开文件,读/写文件,关闭文件. 文件打开之后必须关闭,因为在磁盘上读写文件的功能是由操作系统提供的,文件作为对象,被打开后会占用操作系统的资源,而操作系统在同一 ...
- 用ImageSwitcher实现显示图片(更改图片时有动画效果)
ImageView和ImageSwitcher都是用来显示图片的.只不过后者在更新显示的图片的时候可以有动画效果. 注意点: 1.ImageSwitcher初始化需要定义一个ViewFactory,该 ...
- Prometheus 初体验
本文环境 Redhat Linux 6.7, Prometheus 2.2.1,node_exporter 1.5.2 介绍 Prometheus 是2012年由 SoundCloud 开源的系统监控 ...
- maven配置sqlServer的依赖
配置sqlServer的依赖 <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifac ...
- java含有静态代码块新建的时候报错java.lang.ExceptionInInitializerError
问题描述 最近在写一些单元测试用例,为了避免连接外界服务,所有选择mock了数据库Dao层,计划将数据库所需要的数据存在List中,在类加载的时候初始化List并且填充数据.代码如下: public ...
- [2014亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number
1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 这个题目也是当时笔试第一次见到,花了一个小时才做出了.慢慢总结还是挺简单的. 2.分析 分析如下: (1)一位数N(9 ...
- [leetcode]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- [leetcode]Restore IP Addresses @ Python
原题地址:https://oj.leetcode.com/problems/restore-ip-addresses/ 题意: Given a string containing only digit ...
- C/C++二叉树搜索树操作集
啥是二叉查找树 在数据结构中,有一个奇葩的东西,说它奇葩,那是因为它重要,这就是树.而在树中,二叉树又是当中的贵族.二叉树的一个重要应用是它们在查找中的应用,于是就有了二叉查找树. 使二叉树成为一颗二 ...