尚硅谷之JDBC

117 篇文章 0 订阅
116 篇文章 0 订阅

2、批处理

当需要成批插入或者更新记录时。可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率。

JDBC的批量处理语句包括下面两个方法:

  1. addBatch():添加需要批量处理的SQL语句或参数
  2. executeBatch():执行批量处理语句;

通常我们会遇到两种批量执行SQL语句的情况:

  1. 多条SQL语句的批量处理;
  2. 一个SQL语句的批量传参;

注意:

JDBC连接MySQL时,如果要使用批处理功能,请再url中加参数?rewriteBatchedStatements=true

PreparedStatement作批处理插入时使用values(使用value没有效果)

2.1 Statement

void addBatch(String  sql):添加需要批量处理的SQL语句

int[] executeBatch();执行批量处理语句;

2.2 PreparedStatement

void addBatch()将一组参数添加到此 PreparedStatement 对象的批处理命令中

int[] executeBatch();执行批量处理语句;

2.3 关于效率测试

测试:插入100000条记录

  1. (1)Statement不使用批处理
  2. (2)PreparedStatement不使用批处理
  3. (3)Statement使用批处理
  4. (4)PreparedStatement使用批处理(效率最高)
  1. >(3)>(1)>(2)

2.4 示例代码

package com.atguigu.other;

 

import java.sql.Connection;

import java.sql.PreparedStatement;

 

import org.junit.Test;

 

import com.atguigu.utils.JDBCUtils;

 

public class TestBatch {

         /*

          * 没有使用批处理

          */

         @Test

         public void testNoBatch() throws Exception {

                   long start = System.currentTimeMillis();

                  

                   //批处理

                   //添加500件商品

                   String sql = "INSERT INTO t_goods (pname,price) VALUES(?,?)";

                  

                   Connection conn = JDBCUtils.getConnection();

                   PreparedStatement pst = conn.prepareStatement(sql);

                  

                   for (int i = 1; i <= 100; i++) {

                            String pname = "商品" + i;

                            double price = i;

                           

                            pst.setString(1, pname);

                            pst.setDouble(2, price);

                           

                            int len = pst.executeUpdate();

                            System.out.println("第" +i +"条添加:" + (len>0?"成功":"失败"));

                   }

                  

                   long end = System.currentTimeMillis();

                   System.out.println("耗时:" + (end-start));

         }

        

         @Test

         public void testBatch()throws Exception {

                   long start = System.currentTimeMillis();

                  

                   String sql = "INSERT INTO t_goods (pname,price) VALUES(?,?)";

                  

                   Connection conn = JDBCUtils.getConnection();

                   PreparedStatement pst = conn.prepareStatement(sql);

                   for (int i = 1; i <= 100; i++) {

                            String pname = "商品" + i;

                            double price = i;

                            pst.setObject(1, pname);

                            pst.setObject(2, price);

                           

                            pst.addBatch();//添加到批处理中

                   }

                  

                   int[] executeBatch = pst.executeBatch();//一批命名同时执行

                   for (int i = 0; i < executeBatch.length; i++) {

                            System.out.println("第" +i +"条添加:" + (executeBatch[i]>0?"成功":"失败"));

                   }

                  

                   JDBCUtils.closeQuietly(pst, conn);

                  

                   long end = System.currentTimeMillis();

                   System.out.println("耗时:" + (end-start));

                  

         }

}

 

 

 

 

本教程由尚硅谷教育大数据研究院出品,如需转载请注明来源。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值