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=" ...
随机推荐
- Android之循环显示图像的Android Gallery组件
转自:http://www.blogjava.net/nokiaguy/archive/2010/08/23/329721.html Gallery组件主要用于横向显示图像列表,不过按常规做法.Gal ...
- EditText添加表情
package com.kale.edittext02; import java.lang.reflect.Field; import java.util.Random; import android ...
- 安装部署 Kubernetes 集群
安装部署 Kubernetes 集群 阅读目录: 准备工作 部署 Master 管理节点 部署 Minion 工作节点 部署 Hello World 应用 安装 Dashboard 插件 安装 Hea ...
- .NET零基础入门05:委托与事件
一:前言 本小节,我们需要停一停我们的小游戏开发,虽然它现在还不完美,还很简单,甚至还有BUG.但是,为了更好的理解C#,现在到了该深入了解一些基础知识的时候了. 当然,实际上,本小节内容对于零基础入 ...
- Log Shipping搭建
1. 概述 SQL Server 使用日志传送,您可以自动将“主服务器”实例上“主数据库”内的事务日志备份发送到单独“辅助服务器”实例上的一个或多个“辅助数据库”.事务日志备份分别应用于每个辅助 ...
- 奇怪吸引子---BurkeShaw
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- LeakCanary 内存泄漏 监测 性能优化 简介 原理 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 【Eclipse】Eclipse性能调优
Eclipse性能调优 eclipse 吃内存_百度搜索 eclipse 性能调优之内存分配 - Defonds 的专栏 - CSDN博客 优化JVM参数提高eclipse运行速度 - Java综合 ...
- fasttext使用笔记
http://blog.csdn.net/m0_37306360/article/details/72832606 这里记录使用fastText训练word vector笔记 github地址:htt ...
- Hadoop vs Spark性能对比
http://www.cnblogs.com/jerrylead/archive/2012/08/13/2636149.html Hadoop vs Spark性能对比 基于Spark-0.4和Had ...