第一、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的更多相关文章

  1. 说说Statement、PreparedStatement和CallableStatement的异同(转)

    1.Statement.PreparedStatement和CallableStatement都是接口(interface). 2.Statement继承自Wrapper.PreparedStatem ...

  2. 执行对象Statement、PreparedStatement和CallableStatement详解 JDBC简介(五)

    执行对象是SQL的执行者,SQL是“安排好的任务”,执行对象就是“实际工作的人”. 执行对象有三种: Statement.PreparedStatement和CallableStatement,他们都 ...

  3. Statement、PreparedStatement、CallableStatement的区别

    此三个接口的声明如下: public interface Statement extends Wrapper, AutoCloseable public interface PreparedState ...

  4. Statement、 PreparedStatement 、CallableStatement 区别和联系

    Statement. PreparedStatement .CallableStatement 区别和联系 1. Statement.PreparedStatement和CallableStateme ...

  5. JDBC之Statement、PreparedStatement和CallableStatement

    JDBC提供了Statement.PreparedStatement和CallableStatement三种方式来执行查询语句,其中Statement用于通用查询,PreparedStatement用 ...

  6. 执行对象Statement、PreparedStatement和CallableStatement详解

    执行对象是SQL的执行者,SQL是"安排好的任务",执行对象就是"实际工作的人". 执行对象有三种: Statement.PreparedStatement和C ...

  7. java_JDBC,连接数据库方式,RestSet结果集,Statement,PreparedStatement,事务,批处理,数据库连接池(c3p0和Druid)、Apache-DBUtils、

    一.JDBC的概述 1.JDBC为访问不同的数据薛是供了统一的接口,为使用者屏蔽了细节问题.2. Java程序员使用JDBC,可以连接任何提供了JDBC驱动程序的数据库系统,从而完成对数据库的各种操作 ...

  8. 深入浅出Mybatis系列(七)---mapper映射文件配置之insert、update、delete

    上篇文章<深入浅出Mybatis系列(六)---objectFactory.plugins.mappers简介与配置>简单地给mybatis的配置画上了一个句号.那么从本篇文章开始,将会介 ...

  9. MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存

    二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...

随机推荐

  1. http链接的性能测试工具httping

    安装:MAC环境下使用brew进行安装      brew  install  httping 使用参数: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...

  2. putty adb

    putty.exe -adb -P 5037 transport-usb 网络调试也是可以的 先connect 再执行上面的命令 http://files.cnblogs.com/files/ahuo ...

  3. 用开源项目cropper实现对图片中任意部分进行裁剪

     红色区域为截图控件的区域.    开源项目地址:https://github.com/edmodo/cropper croper这个开源项目可以对一个图片进行任意区域的街区,并且可以设置图片的旋转角 ...

  4. [转]mysql在已有无分区表增加分区,mysql5.5才有,可以是innodb_file_per_table关闭状态.

    FROM : http://blog.csdn.net/sunvince/article/details/7752662 mysql5.1的时候新增的partition,解决了比较简单的shardin ...

  5. kafak-python使用补充

    kafka-python的心跳报文使用的是一个独立的线程,以固定的时间(heartbeat_interval_ms,默认是3000ms)间隔发生心跳信息 member_id唯一标识一个客户端的cons ...

  6. AutoCppHeader AutoHeader 自动根据CPP 或C文件 来生成头文件。

    根据 cpp文件 生成相应的头文件 namespace 声明类定义声明 函数声明 extern 变量声明 选项--------------//[AutoHeader Public] //[AutoHe ...

  7. ASP.NET Razor 简介

    ylbtech-.NET: ASP.NET Razor 简介 Razor 不是一种编程语言.它是服务器端的标记语言. 1. 什么是 Razor?返回顶部 Razor 是一种标记语法,可以让您将基于服务 ...

  8. fasttext介绍和试用

    http://fasttext.apachecn.org/cn/docs/v0.1.0/support.html fasttext介绍网站 https://github.com/facebookres ...

  9. SCRIPT5009: “Sys”未定义 部署.net ajax 解决方案

    今天在部署asp.net ajax 的时候发现部署服务器的时候,ajax刷新不正确,开始以为是System.Web.Extensions没有引用到本地,baidu一圈发现(最近google上不了郁闷~ ...

  10. 遮罩层中的相对定位与绝对定位(Ajax)

    前提:公司最近做的一个项目列表,然后点击项目,出现背景遮罩层,弹出的数据框需要异步加载数据数据,让这个数据框居中,搞了两天终于总算达到Boss满意的程度,做了半年C/S,反过来做B/S,顿时感到技术还 ...