数据库-JDBC批处理

批处理

将多条语句, 放到一起批量处理 .
批处理的原理: 将多条SQL语句, 转换为一个SQL指令. 显著的提高大量SQL语句执行时的数据库性能.

Statement对象使用流程:
1. 得到Statement对象
Statement state = conn.createStatement();
2. 将一条SQL语句, 加入到批处理中.
state.addBatch(String sql);
3. 执行批处理
state.executeBatch();
4. 清空批处理
state.clearBatch();

PreparedStatement对象使用流程:
1. 得到PreparedStatement对象
PreparedStatement state = conn.prepareStatement("预编译的SQL");
2. 填充预编译的参数
state.setXXX(1,填充参数);
3. 将一条填充完毕参数的SQL, 加入到批处理中.
state.addBatch();
4. 执行批处理
state.executeBatch();
5. 清空批处理
state.clearBatch();
  • 案例
package com.java.demo5;

import java.sql.*;

public class Main {
    //statment
    public static void insert1_1() throws SQLException {

        for (int i=0;i<1000;i++) {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123");
            Statement state = conn.createStatement();
            state.executeUpdate("insert into person values('admin','123"+i+"')");
            state.close();
            conn.close();
        }

    }
    //statement 批处理
    public static void insert1_2() throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123");
        Statement state = conn.createStatement();
        for (int i=0;i<1000;i++) {
            //向批处理中 加入一条SQL
            state.addBatch("insert into person values('admin','123"+i+"')");
        }
        //执行批处理
        state.executeBatch();
        //清空批处理指令
        state.clearBatch();
        state.close();
        conn.close();
    }

    //PreparedStatement
    public static void insert2_1() throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123");
        PreparedStatement state = conn.prepareStatement("insert into person values(?,?)");
        for (int i=0;i<10000;i++) {
            state.setString(1,"admin");
            state.setString(2,"123"+i);
            state.executeUpdate();
        }
        state.close();
        conn.close();
    }
    //PreparedStatement批处理
    public static void insert2_2() throws SQLException {
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8", "root", "123");
        PreparedStatement state = conn.prepareStatement("insert into person values(?,?)");
        for (int i=0;i<10000;i++) {
            state.setString(1,"admin");
            state.setString(2,"123"+i);
            state.addBatch();
        }
        //执行批处理
        state.executeBatch();
        //清空批处理指令
        state.clearBatch();
        state.close();
        conn.close();
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值