微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Jdbc系列八:批量处理

 当需要成批插入或者更新记录时。可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率。
 JDBC的批量处理语句包括下面两个方法
  addBatch(String):添加需要批量处理的sql语句或是参数;
  executeBatch():执行批量处理语句;
通常我们会遇到两种批量执行sql语句的情况:
 多条sql语句的批量处理;

在这里插入图片描述


一个sql语句的批量传参;

在这里插入图片描述


在这里插入图片描述


测试:
 向 Oracle 的 customers 数据表中插入 10 万条记录。测试如何插入, 用时最短。
1、使用 Statement,用时 39567ms

@Test
public void testBatchWithStatement(){
    Connection connection = null;
    Statement statement = null;
    String sql = null;

    try {
        connection = JDBCTools.getConnection();
        JDBCTools.beginTx(connection);

        statement = connection.createStatement();

        long begin = System.currentTimeMillis();
        for(int i = 0; i < 100000; i++){
            sql = "INSERT INTO customers VALUES(" + (i + 1)
                    + ", 'name_" + i + "', '29-6月 -13')";
            statement.addBatch(sql);
        }
        long end = System.currentTimeMillis();

        System.out.println("Time: " + (end - begin)); //39567

        JDBCTools.commit(connection);
    } catch (Exception e) {
        e.printstacktrace();
        JDBCTools.rollback(connection);
    } finally{
        JDBCTools.releaseDB(null, statement, connection);
    }
}

2、使用 PreparedStatement。用时 9819ms

@Test
public void testBatchWithPreparedStatement(){
    Connection connection = null;
    PreparedStatement preparedStatement = null;
    String sql = null;

    try {
        connection = JDBCTools.getConnection();
        JDBCTools.beginTx(connection);
        sql = "INSERT INTO customers VALUES(?,?,?)";
        preparedStatement = connection.prepareStatement(sql);
        Date date = new Date(new java.util.Date().getTime());

        long begin = System.currentTimeMillis();
        for(int i = 0; i < 100000; i++){
            preparedStatement.setInt(1, i + 1);
            preparedStatement.setString(2, "name_" + i);
            preparedStatement.setDate(3, date);

            preparedStatement.executeUpdate();
        }
        long end = System.currentTimeMillis();

        System.out.println("Time: " + (end - begin)); //9819

        JDBCTools.commit(connection);
    } catch (Exception e) {
        e.printstacktrace();
        JDBCTools.rollback(connection);
    } finally{
        JDBCTools.releaseDB(null, preparedStatement, connection);
    }
}

3、使用Batch批量操作,用时 569ms

@Test
public void testBatch(){
	Connection connection = null;
	PreparedStatement preparedStatement = null;
	String sql = null;
	
	try {
		connection = JDBCTools.getConnection();
		JDBCTools.beginTx(connection);
		sql = "INSERT INTO customers VALUES(?,?,?)";
		preparedStatement = connection.prepareStatement(sql);
		Date date = new Date(new java.util.Date().getTime());
		
		long begin = System.currentTimeMillis();
		for(int i = 0; i < 100000; i++){
			preparedStatement.setInt(1, i + 1);
			preparedStatement.setString(2, "name_" + i);
			preparedStatement.setDate(3, date);
			
			//"积攒" sql 
			preparedStatement.addBatch();
			
			//当 "积攒" 到一定程度, 就统一的执行一次. 并且清空先前 "积攒" 的 sql
			if((i + 1) % 300 == 0){
				preparedStatement.executeBatch();
				preparedStatement.clearBatch();
			}
		}
		
		//若总条数不是批量数值的整数倍, 则还需要再额外的执行一次. 
		if(100000 % 300 != 0){
			preparedStatement.executeBatch();
			preparedStatement.clearBatch();
		}
		
		long end = System.currentTimeMillis();
		
		System.out.println("Time: " + (end - begin)); //569
		
		JDBCTools.commit(connection);
	} catch (Exception e) {
		e.printstacktrace();
		JDBCTools.rollback(connection);
	} finally{
		JDBCTools.releaseDB(null, preparedStatement, connection);
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐