现在的位置: 首页 > 综合 > 正文

jdbc批处理

2017年11月09日 ⁄ 综合 ⁄ 共 669字 ⁄ 字号 评论关闭
package jdbc.demo;


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

import jdbc.utils.JdbcUtils;

import org.junit.Test;

/**
 * 批处理,在url后加?rewriteBatchedStatements=true开启mysql的批处理
 * @author zzh
 *
 */
public class BatchDemo {
	@Test
	public void demo() throws Exception{
		Connection con = JdbcUtils.getConnection();
		String sql = "insert into stu values (?,?,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		for(int i = 0; i < 1000; i++ ){
			pstmt.setInt(1, i+1);
			pstmt.setString(2, "stu_"+i);
			pstmt.setInt(3, i);
			pstmt.setString(4, i%2==0?"female":"male");
			
			pstmt.addBatch();  //增加批处理
		}
		long start  =System.currentTimeMillis();
		pstmt.executeBatch();  //执行批处理
		long end  =System.currentTimeMillis();
		System.out.println(end-start);

	}
}

抱歉!评论已关闭.