在jdbc2.0里添加了批量处理的功能(batch),其同意将多个sql语句作为一个单元送至数据库去运行,这样做能够提高操作效率。在操作大量的数据时,
ORM框架实现批量是非常慢的。我们能够使用jdbc提供的Batch来提高效率。

演示样例:

首先是使用for循环,一句一句的运行:

public class TestCommon {
static long startTime;
public static void main(String[] args) throws Exception { Connection conn = getConnection();
PreparedStatement ps = null;
try {
startTime=System.nanoTime(); //获取開始时间
ps = conn
.prepareStatement("INSERT INTO batchtab values (?, ? )");
conn.setAutoCommit(false);
for (int n = 0; n < 10000; n++) {
Integer i = new Integer(n);
ps.setString(1, i.toString());
ps.setString(2, "value" + i.toString());
ps.executeUpdate();
}
conn.commit();
long endTime=System.nanoTime(); //获取结束时间
System.out.println("程序执行时间: "+(endTime-startTime)+"ns");
}catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Message: " + ex.getMessage());
System.out.println("Vendor error code: " + ex.getErrorCode());
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception: " + e.getMessage());
} finally {
if (conn != null)
conn.close();
if (ps != null)
ps.close();
}
} public static Connection getConnection() {
Connection con = null; //创建用于连接数据库的Connection对象
try {
Class.forName("com.mysql.jdbc.Driver");// 载入Mysql数据驱动 con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/TestBatch", "root", "123456");// 创建数据连接 } catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
return con; //返回所建立的数据库连接
}
}

使用Batch,批量操作:

public class TestPreStatementBatch {
static long startTime;
public static void main(String[] args) throws Exception { Connection conn = getConnection();
ResultSet rs = null;
PreparedStatement ps=null;
try { startTime=System.nanoTime(); //获取開始时间 ps = conn.prepareStatement("INSERT INTO batchtab values (? , ? )");
conn.setAutoCommit(false);
ps.clearBatch(); for (int n=0; n<10000; n++) {
Integer i = new Integer(n);
ps.setString(1, i.toString());
ps.setString(2, "value" + i.toString());
ps.addBatch();
}
ps.executeBatch();
conn.commit();
long endTime=System.nanoTime(); //获取结束时间
//打印消耗时间
System.out.println("程序执行时间: "+(endTime-startTime)+"ns");
} catch (BatchUpdateException b) {
System.out.println("SQLException: " + b.getMessage());
System.out.println("SQLState: " + b.getSQLState());
System.out.println("Message: " + b.getMessage());
System.out.println("Vendor error code: " + b.getErrorCode());
System.out.print("Update counts: "); } catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("Message: " + ex.getMessage());
System.out.println("Vendor error code: " + ex.getErrorCode());
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception: " + e.getMessage());
} finally {
if( conn != null )
conn.close();
if(ps !=null)
ps.close();
if(rs !=null)
rs.close(); }
} public static Connection getConnection() {
Connection con = null; //创建用于连接数据库的Connection对象
try {
Class.forName("com.mysql.jdbc.Driver");// 载入Mysql数据驱动 con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/TestBatch", "root", "123456");// 创建数据连接 } catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
return con; //返回所建立的数据库连接
}
}

不同点:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2FuZ2xpeDFhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

一条条的循环插入是每插入一条数据都会调用一次运行;而Batch是把全部的数据全都存起来。之后调用一次运行。数据量非常大的话,效率就会差非常多。

数据说话——执行结果:

总结:

通过插入一万条一样的数据消耗的时间,我们能够看到相差的时间。我们能够通过降低语句的多次运行来提高性能。事实上,同.NET中的SqlBulkCopy思想一样。一次运行WriteToServer。就如同生活中我们做事情一样。不能仅仅想做了即可,还要多多思考有没有什么方法能够做到更好。

最新文章

  1. Percona XtraBackup User Manual 阅读笔记
  2. servlet中的转发和重定向问题
  3. phpcms常用函数
  4. OSGI.NET 学习笔记--应用篇
  5. spring 切面 前置后置通知 环绕通知demo
  6. Oracle查询经典
  7. 【HDOJ】2414 Chessboard Dance
  8. 将 Objective-C 代码迁移到 Swift(Swift 2.0更新)-b
  9. js:jquery multiSelect 多选下拉框实例
  10. 浅谈Hybrid技术的设计与实现(转)
  11. 函数响应式编程及ReactiveObjC学习笔记 (-)
  12. [Firewall] iptables Configuration
  13. .net4.5中HttpClient使用注意点
  14. Python+ITchart实现微信中男女比例,城市分布统计并可视化显示
  15. HDU 1556 BIT区间修改+单点查询(fread读入优化)
  16. Hadoop入门
  17. 使用Jenkins遇到的问题
  18. vue 自学笔记(4): 样式绑定与条件渲染
  19. 批量查询&quot;_mget&quot;
  20. Leetcode 117

热门文章

  1. 通过NVM安装node
  2. CodeForces 785A Anton and Polyhedrons
  3. Calendar日期方法
  4. 注入AspectJ切面
  5. Unity 2D游戏开发教程之使用脚本实现游戏逻辑
  6. centos7.3挂在移动硬盘(亲测)
  7. 【BZOJ 4662】 4662: Snow (线段树+并查集)
  8. 「TJOI 2018」游园会 Party
  9. [HAOI2012]外星人
  10. Apache URLRewrite 原理及配置实现