zoukankan      html  css  js  c++  java
  • 数据库程序接口——JDBC——功能第五篇——批量处理

    综述                                                             

        批量处理一般指批量插入,批量更新,删除通过可以指定where条件实现。批量插入的实现方式有三种类型。statement,preparedStatement,callableStatement。

    Java代码

    /**
     * 演示批量插入的三种方式
     * @throws SQLException
     */
    public static void batchInsert() throws SQLException
    {
      // 使用Statement对象进行批量插入,可以执行不同的sql语句
      insertByStatement();
      // 使用PreStatement对象进行批量插入,一条sql语句,多行参数
      insertByPreparedStatement();
      // 使用CallableStatement对象进行批量插入,一条存储过程,多行参数,当表结构修改时,只需要修改存储过程。
      insertByCallableStatement();
    }

     Statement方式

        Statement方式批量执行SQL的优点在于可以执行多条SQL语句,缺点也很明显,需要改变SQL语句时需要修改程序,每次执行都需要重新编译SQL。下面通过举例说明。

        示例演示往数据库student表,book表中批量插入数据。

        student表有四个字段,stu_name(学号,INTEGER类型),stu_name(名称,varchar2(200)),stu_age(年龄,INTEGER类型),stu_class(班级,varchar2(200))。

      CREATE TABLE "STUDENT" 
       (    
           "STU_NUM" NUMBER(*,0), 
        "STU_NAME" VARCHAR2(200), 
        "STU_AGE" NUMBER(*,0), 
        "STU_CLASS" VARCHAR2(200)
       )

    book表中有四个字段,num(编号,INTEGER类型),name(名称,varchar2(200)),type(类型,varchar2(200))。

    CREATE TABLE "SYSTEM"."BOOK" 
    ( 
      "NUM" NUMBER(*,0), 
      "NAME" VARCHAR2(200), 
      "TYPE" VARCHAR2(200)
    )

    Java代码

     /**
       * 通过Statement对象进行批量插入
       * @throws SQLException
       */
      private static void insertByStatement() throws SQLException
      {
        // 获取连接对象
        Connection conn = ConnectionUtil.getConn();
        // 获取Statement对象
        Statement state = conn.createStatement();
        // 不同的sql语句,不同的表, studentSql为插入student表,bookSql为插入book表
        // stu_num(学号),stu_name(名称),stu_age(年龄),stu_class("班级")
        String studentSql = "insert into student values(1,'test1',25,'333班')";
        // num(图书编号),name(名称),type(类型)
        String bookSql = "insert into book values(1,'book1','杂志')";
        // 添加sql语句,
        state.addBatch(studentSql);
        state.addBatch(bookSql);
        // 执行sql
        state.executeBatch();
        // 清空批量
        state.clearBatch();
        // 关闭statement对象
        state.close();
      }

    PreparedStatement方式

          PreparedStatement优点是SQL语句结构不变,并且经过预编译,所以一般适用于一条SQL语句,多行参数。对于插入场景很适用。但是需要知道表中拥有哪些字段,如何设置这些值,如果表结构修改,需要修改代码。

    Java代码

    /**
     * 通过PreparedStatement对象进行批量插入
     * @throws SQLException
     */
    private static void insertByPreparedStatement() throws SQLException
    {
      // 获取连接对象
      Connection conn = ConnectionUtil.getConn();
      // 插入Sql语句
      String insertSql = "insert into student values(?,?,?,?)";
      // 获取PreparedStatement对象
      PreparedStatement preStatement = conn.prepareStatement(insertSql);
      // 设置参数
      for(int i=2;i<100;i++)
      {
        // 添加批量执行批量,相比于Statement的方式,一条sql语句,多行参数,适用于单表插入
        setPreparedStatementParam(preStatement, i, "test"+i, 23, "333班");
        preStatement.addBatch();
      }
    
      preStatement.executeBatch();
      // 清空批量,关闭对象
      preStatement.clearBatch();
      preStatement.close();
    }
    
    /**
     * 为PreparedStatement对象设置参数
     * @param preStatement PreParedStament对象
     * @param stuNum 学号
     * @param name 姓名
     * @param age 年龄
     * @param stuClass 班级
     * @throws SQLException 
     */
    private static void setPreparedStatementParam(PreparedStatement preStatement, int stuNum, String name, int age,
            String stuClass) throws SQLException
    {
      preStatement.setInt(1, stuNum);
      preStatement.setString(2, name);
      preStatement.setInt(3, age);
      preStatement.setString(4, stuClass);
    }

    CallableStatement方式

        CallableStatement的方式优点在于只需要了解存储过程中的使用,不需要了解表结构,当表结构改变时,不用修改程序.

    存储过程

    create or replace procedure insertStudentProc
    (stu_num in INTEGER,name in varchar2)
    as 
    begin
      insert into student values(stu_num,name,25,'存储过程');
      commit;
    end;

    Java代码

    /**
     * 通过CallableStatement对象进行批量插入
     * @throws SQLException 
     */
    private static void insertByCallableStatement() throws SQLException 
    {
      // 获取连接对象
      Connection conn = ConnectionUtil.getConn();
      // 第一个参数为学号,第二个参数为名称
      String callSql = "{call insertStudentProc(?,?)}";
      // 创建CallableStatement对象
      CallableStatement callStatement = conn.prepareCall(callSql);
      // 设置参数
      for(int i=100;i<200;i++)
      {
        setCallableStatementParam(callStatement, i, "test"+i);
        callStatement.addBatch();
      }
      // 执行批量操作
      callStatement.executeBatch();
      // 清空批量,关闭对象
      callStatement.clearBatch();
      callStatement.close();
    }
    
    
    /**
     * 为CallableStatement对象设置参数
     * @param callStatement CallableStatement对象
     * @param stuNum 学号
     * @param name 姓名
     * @throws SQLException 
     */
    private static void setCallableStatementParam(CallableStatement callStatement,int stuNum,String name) throws SQLException
    {
      callStatement.setInt(1, stuNum);
      callStatement.setString(2, name); 
    }

    至此本篇内容结束

  • 相关阅读:
    vue项目引用less报错
    vue dev配置代理会报404
    为什么需要用到消息队列
    理解kafka消费者
    WebSocket和long poll、ajax轮询的区别
    数据库开发——MySQL——慢查询优化
    数据库开发——MySQL——索引原理
    数据库开发——MySQL——函数与流程控制
    数据库开发——MySQL——内置功能
    数据库开发——MySQL——pymysql模块
  • 原文地址:https://www.cnblogs.com/rain144576/p/6971656.html
Copyright © 2011-2022 走看看