批处理

批处理

package com.hcx.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Date;

import org.junit.Test;

import com.hcx.utils.JdbcUtils;

public class BatchTest {

	// 通过Statement命令对象演示一个插入3条记录
	@Test
	public void testStatement() throws Exception {
		// 获得链接对象
		Connection conn = JdbcUtils.getConnection();
		// 创建预处理命令对象
		Statement stmt = conn.createStatement();

		String sql1 = "insert into t3 values(1,'令狐冲',20)";
		String sql2 = "insert into t3 values(2,'杨过',23)";
		String sql3 = "insert into t3 values(3,'乔峰',24)";

		// 将三条sql语句一起放入stmt对象中,一起发送到服务执行,在DBMS中会编译成一个逻辑执行单元,所以速度会更快一些
		stmt.addBatch(sql1);
		stmt.addBatch(sql2);
		stmt.addBatch(sql3);

		// 执行sql语句
		stmt.executeBatch();
		// 释放资源
		JdbcUtils.release(null, stmt, conn);
	}

	// 通过PreparedStatement对象插入三条数据
	@Test
	public void testPreparedStatement() throws Exception {
		// 获得链接对象
		Connection conn = JdbcUtils.getConnection();
		// 创建预处理命令对象
		PreparedStatement pstmt = conn
				.prepareStatement("insert into t3 values(?,?,?)");
		// 指定?的值
		pstmt.setInt(1, 4);
		pstmt.setString(2, "黄蓉");
		pstmt.setInt(3, 18);

		pstmt.addBatch();
		// 指定?的值
		pstmt.setInt(1, 5);
		pstmt.setString(2, "小龙女");
		pstmt.setInt(3, 17);

		pstmt.addBatch();
		// 指定?的值
		pstmt.setInt(1, 6);
		pstmt.setString(2, "赵敏");
		pstmt.setInt(3, 18);

		pstmt.addBatch();
		// 执行sql语句
		pstmt.executeBatch();
		// 释放资源
		JdbcUtils.release(null, pstmt, conn);
	}

	// 通过PreparedStatement对象插入1000条数据
	@Test
	public void testPreparedStatement1() throws Exception {
		// 获得链接对象
		Connection conn = JdbcUtils.getConnection();
		System.out.println(new Date());
		// 创建预处理命令对象
		PreparedStatement pstmt = conn
				.prepareStatement("insert into t3 values(?,?,?)");
		
		for (int i = 0; i < 1000; i++) {
			// 指定?的值
			pstmt.setInt(1, i);
			pstmt.setString(2, "黄蓉" + i);
			pstmt.setInt(3, i);
			
			pstmt.addBatch();
			
			if(i%200 == 0){
				//执行sql语句
				pstmt.executeBatch() ;
				//一定要清空缓存
				pstmt.clearBatch() ;
			}	
		}

		//为了防止缓存中还有sql没有执行,应当再次 执行sql语句
		pstmt.executeBatch();
		System.out.println(new Date());
		// 释放资源
		JdbcUtils.release(null, pstmt, conn);
	}

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值