当前位置:Gxlcms > 数据库问题 > DRP——JDBC中的Batch

DRP——JDBC中的Batch

时间:2021-07-01 10:21:17 帮助过:23人阅读

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

演示样例:

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

  1. public class TestCommon {
  2. static long startTime;
  3. public static void main(String[] args) throws Exception {
  4. Connection conn = getConnection();
  5. PreparedStatement ps = null;
  6. try {
  7. startTime=System.nanoTime(); //获取開始时间
  8. ps = conn
  9. .prepareStatement("INSERT INTO batchtab values (?, ?<p></p><p>)");
  10. conn.setAutoCommit(false);
  11. for (int n = 0; n < 10000; n++) {
  12. Integer i = new Integer(n);
  13. ps.setString(1, i.toString());
  14. ps.setString(2, "value" + i.toString());
  15. ps.executeUpdate();
  16. }
  17. conn.commit();
  18. long endTime=System.nanoTime(); //获取结束时间
  19. System.out.println("程序执行时间: "+(endTime-startTime)+"ns");
  20. }catch (SQLException ex) {
  21. System.out.println("SQLException: " + ex.getMessage());
  22. System.out.println("SQLState: " + ex.getSQLState());
  23. System.out.println("Message: " + ex.getMessage());
  24. System.out.println("Vendor error code: " + ex.getErrorCode());
  25. } catch (Exception e) {
  26. e.printStackTrace();
  27. System.err.println("Exception: " + e.getMessage());
  28. } finally {
  29. if (conn != null)
  30. conn.close();
  31. if (ps != null)
  32. ps.close();
  33. }
  34. }
  35. public static Connection getConnection() {
  36. Connection con = null; //创建用于连接数据库的Connection对象
  37. try {
  38. Class.forName("com.mysql.jdbc.Driver");// 载入Mysql数据驱动
  39. con = DriverManager.getConnection(
  40. "jdbc:mysql://localhost:3306/TestBatch", "root", "123456");// 创建数据连接
  41. } catch (Exception e) {
  42. System.out.println("数据库连接失败" + e.getMessage());
  43. }
  44. return con; //返回所建立的数据库连接
  45. }
  46. } </p>

使用Batch,批量操作:

  1. public class TestPreStatementBatch {
  2. static long startTime;
  3. public static void main(String[] args) throws Exception {
  4. Connection conn = getConnection();
  5. ResultSet rs = null;
  6. PreparedStatement ps=null;
  7. try {
  8. startTime=System.nanoTime(); //获取開始时间
  9. ps = conn.prepareStatement("INSERT INTO batchtab values (?<p></p><p>, ?</p><p>)");
  10. conn.setAutoCommit(false);
  11. ps.clearBatch();
  12. for (int n=0; n<10000; n++) {
  13. Integer i = new Integer(n);
  14. ps.setString(1, i.toString());
  15. ps.setString(2, "value" + i.toString());
  16. ps.addBatch();
  17. }
  18. ps.executeBatch();
  19. conn.commit();
  20. long endTime=System.nanoTime(); //获取结束时间
  21. //打印消耗时间
  22. System.out.println("程序执行时间: "+(endTime-startTime)+"ns");
  23. } catch (BatchUpdateException b) {
  24. System.out.println("SQLException: " + b.getMessage());
  25. System.out.println("SQLState: " + b.getSQLState());
  26. System.out.println("Message: " + b.getMessage());
  27. System.out.println("Vendor error code: " + b.getErrorCode());
  28. System.out.print("Update counts: ");
  29. } catch (SQLException ex) {
  30. System.out.println("SQLException: " + ex.getMessage());
  31. System.out.println("SQLState: " + ex.getSQLState());
  32. System.out.println("Message: " + ex.getMessage());
  33. System.out.println("Vendor error code: " + ex.getErrorCode());
  34. } catch (Exception e) {
  35. e.printStackTrace();
  36. System.err.println("Exception: " + e.getMessage());
  37. } finally {
  38. if( conn != null )
  39. conn.close();
  40. if(ps !=null)
  41. ps.close();
  42. if(rs !=null)
  43. rs.close();
  44. }
  45. }
  46. public static Connection getConnection() {
  47. Connection con = null; //创建用于连接数据库的Connection对象
  48. try {
  49. Class.forName("com.mysql.jdbc.Driver");// 载入Mysql数据驱动
  50. con = DriverManager.getConnection(
  51. "jdbc:mysql://localhost:3306/TestBatch", "root", "123456");// 创建数据连接
  52. } catch (Exception e) {
  53. System.out.println("数据库连接失败" + e.getMessage());
  54. }
  55. return con; //返回所建立的数据库连接
  56. }
  57. }</p>

不同点:

技术分享

 

    技术分享

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

 

数据说话——执行结果:

 

技术分享

技术分享

 

总结: 

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


DRP——JDBC中的Batch

标签:new   大量   content   方法   count   commit   throw   exec   分享   

人气教程排行