比较批量处理与非批量处理

//非批量处理
        long l1 = System.currentTimeMillis();
        AccounterServiceImpl acctServImpl = new AccounterServiceImpl();
        for (int i = 0; i < 30; i++) {
        //插入30条数据
            acctServImpl.regester(new Accounter(0, "u"+i, "123"));
        }
        long l2 = System.currentTimeMillis();
        System.out.println("非批处理插入30个用户所花时间:" +(l2-l1));//926

//批量处理
        Connection conn = null;
        Statement stat = null;
        try {
            conn = DbUtils2_Dbcp.getMysqlConn();
            stat = conn.createStatement();
            conn.setAutoCommit(false);
            for (int i = 0; i < 200; i++) {
            //建立200个插入的批处理
                stat.addBatch("insert into accounter(accounter_name,accounter_psw) values('u"+(i)+"','123')");
            }
            //执行批处理
            stat.executeBatch();
            stat.clearBatch();
            conn.commit();
            ..catch-finally....
        long l3 = System.currentTimeMillis();
        System.out.println("批处理插入200个用户所花时间:" +(l3-l2));

这里写图片描述
总结;虽然批处理任务大于非批处理,但是执行效率还是高的很的。
批处理有批处理的好处,但是批处理时一般不使用PreparementStatment。但是如果使用Statement的话就要小心sql注入问题。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值