数据库批处理和一般处理的快慢比较

 下面有两个方法:ct1方法是标准的处理模式,ct2方法是批处理的模式。

最终显示:批处理模式的速度比一般处理模式块的很多,在实际开发中,一个表的字段越多,一般二三十个很常见,SQL语句越复杂,越能体现出批处理的速度之快!

package com.wei.employee.common;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;

/**
 * 批处理模式 和一般处理模式下 比较之下 看谁的执行效率快
 */
public class BatchSample {
    //标注模式下的处理模式
    public static void tc1(){

        Connection con=null;
        PreparedStatement pstmt=null;
        try {
            long statrTime=new Date().getTime();
            con = DbUtils.getConnection();
            //JDBC默认使用自动提交的模式
            con.setAutoCommit(false);//关闭自动提交模式
            String sql="insert into employee(eno,ename,salary,dname)values(?,?,?,?)";
            for (int i = 100000; i < 200000; i++) {
//                if (i==1005){
//                    throw  new Exception("插入失败");
//                }
                pstmt = con.prepareStatement(sql);
                pstmt.setInt(1,i);
                pstmt.setString(2,"员工"+i);
                pstmt.setFloat(3,40000f);
                pstmt.setString(4,"研发部");
                pstmt.executeUpdate();
            }
            con.commit();//提交数据
            long endTime=new Date().getTime();
            System.out.println("tc1方法的执行总时长:"+(endTime-statrTime));
        } catch (Exception e) {
            e.printStackTrace();
            try {
                if (con!=null&&!con.isClosed()){
                    con.rollback();//回滚数据
                }

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }finally {
            DbUtils.closeConnection(null,pstmt,con);
        }
    }

    //批处理模式下执行的的时间
    public static void tc2(){

        Connection con=null;
        PreparedStatement pstmt=null;
        try {
            long statrTime=new Date().getTime();
            con = DbUtils.getConnection();
            //JDBC默认使用自动提交的模式
            con.setAutoCommit(false);//关闭自动提交模式
            String sql="insert into employee(eno,ename,salary,dname)values(?,?,?,?)";
            pstmt = con.prepareStatement(sql);
            for (int i = 200000; i < 300000; i++) {
//                if (i==1005){
//                    throw  new Exception("插入失败");
//                }
                pstmt.setInt(1,i);
                pstmt.setString(2,"员工"+i);
                pstmt.setFloat(3,40000f);
                pstmt.setString(4,"研发部");
                pstmt.addBatch();//将刚才设置的参数加入批处理的任务中
//                pstmt.executeUpdate();
            }

            pstmt.executeBatch();//执行批处理任务
            con.commit();//提交数据
            long endTime=new Date().getTime();
            System.out.println("tc2方法的执行总时长:"+(endTime-statrTime));
        } catch (Exception e) {
            e.printStackTrace();
            try {
                if (con!=null&&!con.isClosed()){
                    con.rollback();//回滚数据
                }

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }finally {
            DbUtils.closeConnection(null,pstmt,con);
        }
    }
    public static void main(String[] args) {

        tc1();
        tc2();

    }
}

运行截图:

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阳光不锈@

如果有帮助的话,打赏一下吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值