JDBC学习日记--批量数据处理
2020-01-15 11:39
661 查看
三种批处理方式的比较:
数据表TableA(no,name)
一、用Statement
public void case1() { //声明Connection Connection connection = null; //声明Statement Statement stms = null; try { //得到Connection connection = JDBCTools.getConnection(); //取消默认提交 JDBCTools.cancelAuto(connection); String sql = null; //得到Statement stms = connection.createStatement(); long begin = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { //写SQL sql = "Insert into TableA values(" + i + ",'name_" + i + "')"; //执行 stms.executeUpdate(sql); } long end = System.currentTimeMillis(); //打印执行时间 System.err.println("Time : " + (end - begin));// 21678 //提交事务 JDBCTools.commit(connection); } catch (Exception e) { //回滚事务 JDBCTools.rollBack(connection); // TODO: handle exception } finally { JDBCTools.closeResource(connection, stms, null); } }
用时:21678
二、用PreparedStatement
public void case2() { //声明Connection Connection connection = null; //声明Statement PreparedStatement ps = null; try { //得到Connection connection = JDBCTools.getConnection(); //取消默认提交 JDBCTools.cancelAuto(connection); String sql = null; sql = "Insert into TableA values(?,?)"; //得到PreparedStatement ps = connection.prepareStatement(sql); long begin = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { ps.setObject(1, i); ps.setObject(2, "name_" + i); //执行 ps.executeUpdate(); } long end = System.currentTimeMillis(); //打印执行时间 System.err.println("Time : " + (end - begin));// 16721 //提交事务 JDBCTools.commit(connection); } catch (Exception e) { //回滚事务 JDBCTools.rollBack(connection); // TODO: handle exception } finally { JDBCTools.closeResource(connection, ps, null); } }
用时:16721
三、用Batch
public void case3() { //声明Connection Connection connection = null; //声明Statement PreparedStatement ps = null; try { //得到Connection connection = JDBCTools.getConnection(); //取消默认提交 JDBCTools.cancelAuto(connection); String sql = null; sql = "Insert into TableA values(?,?)"; //得到PreparedStatement ps = connection.prepareStatement(sql); long begin = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { ps.setObject(1, i); ps.setObject(2, "name_" + i); //积攒SQL ps.addBatch(); //存到一定数量就统一执行一次 if ((i+1) % 300 == 0) { //执行batch ps.executeBatch(); //清空batch ps.clearBatch(); } } //最终没有足够积累也执行 if (100000 % 300 != 0) { //执行batch ps.executeBatch(); //清空batch ps.clearBatch(); } long end = System.currentTimeMillis(); //打印执行时间 System.err.println("Time : " + (end - begin));// 3184 //提交事务 JDBCTools.commit(connection); } catch (Exception e) { //回滚事务 JDBCTools.rollBack(connection); // TODO: handle exception } finally { JDBCTools.closeResource(connection, ps, null); } }
用时:3184
使用batch小结:
PreparedStatement ps = conn.prepareStatement(sql);
-
//积累数据
ps.addBatch();
-
//执行批操作
ps.executeBatch();
-
//清空batch
ps.clearBatch();
数据库命令
-
//计算元组数量
select COUNT(*) from TableA
-
//清空表
truncate table TableA
- 点赞 1
- 收藏
- 分享
- 文章举报
相关文章推荐
- JDBC以及相关技术学习(六)----批量SQL处理
- 零基础学习java日记第二十二天JDBC
- 零基础学习java日记第二十三天JDBC
- JAVA 学习日记(4).JDBC
- JAVA 学习日记(5).JSP,JDBC数据库查询
- IDBC学习(5)JDBC字节流数据处理
- Java学习日记番外篇:jdbc基础
- 批量生成用户中 --------<<鸟哥的私房菜>>学习日记之一
- 2017.01.01 学习日记-备忘录模式
- 黑马程序员-讲解Co a5df lletion共性方法学习日记
- linux学习日记九 正则表达式介绍
- javaee学习之路(二十)JDBC项目实战--员工信息存储系统
- 黑马程序员--Java学习日记之集合(collection类与list接口)及泛型概述
- 黑马程序员-去除ArrayList集合中的重复元素学习日记
- 2016年06月08日学习日记c语言指针
- java学习日记_55:Scanner的概述和构造方法原理
- SQLCookBook第四章学习日记11
- Makefile 学习日记(六)——.c和.h不在同路径下的编译
- 我的第一份学习日记(2019.4.19)
- c# 委托的学习日记