日期:2014-05-16  浏览次数:20454 次

jdbc批处理ad使用
package Jdbcday02;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class TestBatchAdvance {
	public static void main(String[] args) {
		testBatch();
	}
	public static void testBatch(){
		String sql="insert into mytemp values(?,?)";
		try {
			Connection conn=
				ConnectionUtils.openConnection();
			conn.setAutoCommit(false);
			PreparedStatement psmt=conn.prepareStatement(sql);
					for (int i = 0; i < 10000; i++) {
						psmt.setInt(1, i);
						psmt.setString(2, "values"+i);
						psmt.addBatch();			//添加到批处理上
						if (i%1000==0) {
							psmt.executeBatch();		//每1000次一提交
						}
						psmt.executeBatch();
					}
			conn.commit();
			ConnectionUtils.closeStatement(psmt);
			ConnectionUtils.closeConnection(conn);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}