JDBC 批量高效插入数据

1.批量执行SQL语句

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

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

  • addBatch(String):添加需要批量处理的SQL语句或是参数;
  • executeBatch():执行批量处理语句;
  • clearBatch():清空缓存的数据

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

  • 多条SQL语句的批量处理;
  • 一个SQL语句的批量传参;

2 .高效的批量插入

举例:向数据表中插入20000条数据

  • 数据库中提供一个goods表。创建如下:
    在这里插入图片描述

3.代码实现

package com.atguigu5.blob;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.junit.Test;

import com.atguigu3.util.JDBCUtils;

/*
 * 使用PreparesStatement实现批量数据操作
 * upadte delete本身就具有批量操作的效果
 * 此时的批量操作主要指的是批量插入,使用PreparedStatement如何实现更高效的批量插入
 * 
 * 题目:向goods中插入20000条数据
 * CREATE TABLE goods(
	id INT PRIMARY KEY auto_increment,
	NAME VARCHAR(25) 
	);
 * 方式一:使用Statement
 * Connection conn=JDBCUtils.getConnection;
 * Statement st= conn.createStatement();
 * for(int i=1;i<=20000;i++){
 * 	String sql="insert into goods(name)values('name_"+i+"')";
 * 	st.execute(sql);
 * }
 * 
 * 
 * 
 * 
 */
public class InsertTest {
	//批量插入的方式二:使用PreparesStatement
	@Test
	public void testInsert1()  {
		Connection conn=null;
		PreparedStatement ps=null;
		try {
			long start = System.currentTimeMillis();
			conn = JDBCUtils.getConnection();
			
			String sql="insert into goods(name)values(?)";
			ps = conn.prepareStatement(sql);
			
			for (int i = 0; i < 20000; i++) {
				ps.setObject(1,"name_"+i);
				ps.execute();
			}
			long end = System.currentTimeMillis();

			System.out.println("花费的时间为:"+(end-start));
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			JDBCUtils.closeResource(conn, ps);

		}
	}
	
	/*
	 *批量插入的方式三:
	 *addBatch(String):添加需要批量处理的SQL语句或是参数;
	 *executeBatch():执行批量处理语句;
	 *clearBatch():清空缓存的数据
	 *2、mysql服务器默认是关闭批处理的,我们需要通过一个参数,让mysql开启批处理的支持。
	 * 		 ?rewriteBatchedStatements=true 写在配置文件的url后面
	 * 
	 */
	@Test
	public void testInsert2() {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			
			long start = System.currentTimeMillis();
			
			conn = JDBCUtils.getConnection();
			String sql = "insert into goods(name)values(?)";
			ps = conn.prepareStatement(sql);
			for(int i = 1;i <= 1000000;i++){
				ps.setObject(1, "name_" + i);
				
				//1."攒"sql
				ps.addBatch();
				
				if(i % 500 == 0){
					//2.执行batch
					ps.executeBatch();
					
					//3.清空batch
					ps.clearBatch();
				}
				
			}
			
			long end = System.currentTimeMillis();
			
			System.out.println("花费的时间为:" + (end - start));//20000:83065 -- 565
		} catch (Exception e) {								//1000000:16086
			e.printStackTrace();
		}finally{
			JDBCUtils.closeResource(conn, ps);
			
		}
		
	}
	//批量插入的方式四:设置连接不允许自动提交数据
	@Test
	public void testInsert3() {
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			
			long start = System.currentTimeMillis();
			
			conn = JDBCUtils.getConnection();
			//设置不允许自动提交
			conn.setAutoCommit(false);
			String sql = "insert into goods(name)values(?)";
			ps = conn.prepareStatement(sql);
			for(int i = 1;i <= 1000000;i++){
				ps.setObject(1, "name_" + i);
				
				//1."攒"sql
				ps.addBatch();
				
				if(i % 500 == 0){
					//2.执行batch
					ps.executeBatch();
					
					//3.清空batch
					ps.clearBatch();
				}
				
			}
			//提交数据
			conn.commit();
			
			long end = System.currentTimeMillis();
			
			System.out.println("花费的时间为:" + (end - start));//20000:83065 -- 565
		} catch (Exception e) {								//1000000:16086
			e.printStackTrace();
		}finally{
			JDBCUtils.closeResource(conn, ps);
			
		}
		
	}
	
}

4.方法三种批处理的配置文件:

在这里插入图片描述

原文地址:https://www.cnblogs.com/James-221/p/13647464.html