对MySQL数据库进行批量处理操作

目录

1.使用Statement批量插入数据:

2.使用PreparedStatement批量插入数据:

3.在2的基础上添加Batch缓存:

4.在3的基础上增加取消自动提交数据的功能


由于数据库本身就可以批量进行选择(例如 select * from customers),

删除(例如delete * from customers)。所以本章主要讲解数据库如何实现数据的批量插入

1.使用Statement批量插入数据:

package test_batch_insert;
import java.sql.Connection;
import java.sql.Statement;

/*
 * 使用Statement对象插入1000条数据
 * 效率太慢,不必推荐使用			花费:40993毫秒
 */
import org.junit.Test;

import myjdbc.utils.JDBCUtil;

public class TestStatementInsert {
	@Test
	public void testInsert() throws Exception {
		Connection conn = JDBCUtil.getConnection();
		Statement statement = conn.createStatement();
		long t1 = System.currentTimeMillis();
		for(int i=0;i<1000;i++) {
			String sql = "insert into aa(name) values('name_" + i +"')";
			statement.execute(sql);
		}
		
		long t2 = System.currentTimeMillis();
		System.out.println(t2-t1);
		statement.close();
	}
}

2.使用PreparedStatement批量插入数据:

package test_batch_insert;
/*
 * 使用Statement批处理(插入)1000条数据
 * 花费:40851ms
 */
import java.sql.Connection;
import java.sql.PreparedStatement;

import org.junit.Test;

import myjdbc.utils.JDBCUtil;

public class TestPreparedStatementInsert {
	@Test
	public void testInsert() throws Exception {
		//获取连接
		Connection conn = JDBCUtil.getConnection();
		//获取PreparedStatement实例
		String sql = "insert into aa(name) values( ? )";
		PreparedStatement ps = conn.prepareStatement(sql);
		//计算运行时间
		long t1 = System.currentTimeMillis();
		
		//执行
		for(int i=1;i<=1000;i++) {
			
			ps.setObject(1, "name2_"+i);
			ps.execute();
		}
		
		
		
		long t2 = System.currentTimeMillis();
		
		System.out.println(t2-t1);
		//关闭资源
		JDBCUtil.closeResource(conn, ps);
	}
}

3.在2的基础上添加Batch缓存:

package test_batch_insert;
/*
 * 使用Batch缓存,攒SQL语句,然后在一起提交执行,类似于IO流中的以Byte数组传流
 * 
 * MySQL服务器默认是关闭批处理的,我们需要一个参数,让MySQL开启批处理的支持
 * 在配置文件url后面加上		?rewriteBatchedStatements=true
 *或者使用更新的MySQL驱动:mysql-connector-java-5.1.37-bin.java
 * 
 * 花费:41590ms	

 */
import java.sql.Connection;
import java.sql.PreparedStatement;

import org.junit.Test;

import myjdbc.utils.JDBCUtil;

public class TestPreparedStatementInsert02 {
	@Test
	public void testInsert() throws Exception {
		//获取连接
		Connection conn = JDBCUtil.getConnection();
		//获取PreparedStatement实例
		String sql = "insert into aa(name) values( ? )";
		PreparedStatement ps = conn.prepareStatement(sql);
		//计算运行时间
		long t1 = System.currentTimeMillis();
		
		//执行
		for(int i=1;i<=1000;i++) {
			
			ps.setObject(1, "name3_"+i);
			
			//1.攒SQL
			ps.addBatch();
			
			if(i%500==0) {
				//执行Batch
				ps.executeBatch();
				//清空Batch
				ps.clearBatch();
			}
			
			
		}
		
		
		
		long t2 = System.currentTimeMillis();
		
		System.out.println(t2-t1);
		//关闭资源
		JDBCUtil.closeResource(conn, ps);
	}
}

4.在3的基础上增加取消自动提交数据的功能

package test_batch_insert;
/*
 * 使用Batch缓存,攒SQL语句,然后在一起提交执行,类似于IO流中的以Byte数组传流
 * 
 * 设置不允许自动提交数据	conn.setAutoCommit(false);
 * 提交数据	conn.commit();
 * 
 * 花费:440ms	
 *
 */
import java.sql.Connection;
import java.sql.PreparedStatement;

import org.junit.Test;

import myjdbc.utils.JDBCUtil;

public class TestPreparedStatementInsert03 {
	@Test
	public void testInsert() throws Exception {
		//获取连接
		Connection conn = JDBCUtil.getConnection();
		
		//设置不允许自动提交数据
		conn.setAutoCommit(false);
		
		//获取PreparedStatement实例
		String sql = "insert into aa(name) values( ? )";
		PreparedStatement ps = conn.prepareStatement(sql);
		//计算运行时间
		long t1 = System.currentTimeMillis();
		
		//执行
		for(int i=1;i<=1000;i++) {
			
			ps.setObject(1, "name3_"+i);
			
			//1.攒SQL
			ps.addBatch();
			
			if(i%500==0) {
				//执行Batch
				ps.executeBatch();
				//清空Batch
				ps.clearBatch();
			}
			
			
		}
		
		//提交数据
		conn.commit();
		
		long t2 = System.currentTimeMillis();
		
		System.out.println(t2-t1);
		//关闭资源
		JDBCUtil.closeResource(conn, ps);
	}
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值