批处理

本文介绍了JDBC编程中两种批处理操作的方法:使用Statement接口和PreparedStatement接口。对比了这两种方式的优劣,并通过实例展示了使用PreparedStatement进行批处理可以显著提高性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

批处理
关于JDBC编程批处理操作

第一种方式:使用Statement接口的批处理
addBatch(sql) 将一条sql 加入批处理到缓存
executeBatch() 执行批处理 将这组sql一次性发送数据库
clearBatch() 清除批处理缓存
缺点:如果sql结构都一样
Insert into user(name,password) values(‘aa’,’111’);
Insert into user(name,password) values(‘bb’,’222’);
Insert into user(name,password) values(‘cc’,’333’);
Insert into user(name,password) values(‘dd’,’444’);
会导致数据库编译sql语句四次 —- 性能比较差

第二种方式:使用PreparedStatement进行批处理
好处:如果连续执行多条结构相同sql — 采用预编译 —- SQL只需要编译一次

案例:向数据库插入50000条数据
create table person(
id int primary key,
name varchar(40),
email varchar(100)
);

mysql 50000 — 59秒
Oracle 50000 — 906毫秒 100000 — 1328毫秒

如果sql 结构都相同 — PreparedStatement 批处理
如果sql 结构存在不同 — Statement 批处理

public void demo1() {
        // 使用Statement接口 批处理操作
        // 批处理操作一般没有查询语句, insert update delete其它sql
        Connection conn = null;
        Statement stmt = null;

        try {
            conn = JDBCUtils.getConnection();
            stmt = conn.createStatement();

            // 连续执行多条sql
            stmt.addBatch("create database mytest");
            stmt.addBatch("use mytest");
            stmt.addBatch("create table mytable(id int , name varchar(20))");
            stmt.addBatch("insert into mytable values(1,'aaa')");
            stmt.addBatch("insert into mytable values(2,'bbb')");
            stmt.addBatch("insert into mytable values(3,'ccc')");
            stmt.addBatch("insert into mytable values(4,'ddd')");

            stmt.addBatch("update mytable set name ='abcd' where id = 2");
            stmt.addBatch("delete from mytable where id =3 ");

            stmt.executeBatch();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(stmt, conn);
        }

    }
public void demo2() {
        long start = System.currentTimeMillis();
        // 向mysql 通过PreparedStatement 插入50000条数据
        Connection conn = null;
        PreparedStatement stmt = null;

        String sql = "insert into person values(?,?,?)";
        try {
            conn = JDBCUtils.getConnection();
            stmt = conn.prepareStatement(sql);// 预编译
            for (int i = 1; i <= 100000; i++) {
                stmt.setInt(1, i);
                stmt.setString(2, "name" + i);
                stmt.setString(3, "email" + i);
                // 将刚刚设置参数 加入批处理
                stmt.addBatch();

                if (i % 1000 == 0) { // 每1000条向数据库传递一次
                    stmt.executeBatch();
                    stmt.clearBatch();
                }
            }

            // 为了确保 缓存没有sql
            stmt.executeBatch();

            long end = System.currentTimeMillis();
            System.out.println("执行时间:" + (end - start));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.release(stmt, conn);
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值