尚硅谷之JDBC

2、批处理

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

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

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

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

  • 多条SQL语句的批量处理;
  • 一个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)Statement不使用批处理
  • (2)PreparedStatement不使用批处理
  • (3)Statement使用批处理
  • (4)PreparedStatement使用批处理(效率最高)
  • >(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));

 

         }

}