微信登录

JDBC - PS - 增批量

插入20000条数据

方式 理解 处理条数 时间/秒 处理条数 时间/秒
方式一 使用Statement for 循环
方式二 使用PreparedStatement for 循环 20000 83秒
方式三 mysql开启批处理 20000 0.56秒 1000000 16秒
方式四 设置连接不允许自动提交数据 1000000 5秒
  1. package com.atguigu5.blob;
  2. import java.sql.Connection;
  3. import java.sql.PreparedStatement;
  4. import java.sql.SQLException;
  5. import org.junit.Test;
  6. import com.atguigu3.util.JDBCUtils;
  7. /*
  8. * 使用PreparedStatement实现批量数据的操作
  9. *
  10. * update、delete本身就具有批量操作的效果。
  11. * 此时的批量操作,主要指的是批量插入。使用PreparedStatement如何实现更高效的批量插入?
  12. *
  13. * 题目:向goods表中插入20000条数据
  14. * CREATE TABLE goods(
  15. id INT PRIMARY KEY AUTO_INCREMENT,
  16. NAME VARCHAR(25)
  17. );
  18. * 方式一:使用Statement
  19. * Connection conn = JDBCUtils.getConnection();
  20. * Statement st = conn.createStatement();
  21. * for(int i = 1;i <= 20000;i++){
  22. name = name_" + i + "
  23. * String sql = "insert into goods(name)values(" + name + ")";
  24. * st.execute(sql);
  25. * }
  26. *
  27. */
  28. public class InsertTest {
  29. //批量插入的方式二:使用PreparedStatement
  30. @Test
  31. public void testInsert1() {
  32. Connection conn = null;
  33. PreparedStatement ps = null;
  34. try {
  35. long start = System.currentTimeMillis();
  36. conn = JDBCUtils.getConnection();
  37. String sql = "insert into goods(name)values(?)";
  38. ps = conn.prepareStatement(sql);
  39. for(int i = 1;i <= 20000;i++){
  40. ps.setObject(1, "name_" + i);
  41. ps.execute();
  42. }
  43. long end = System.currentTimeMillis();
  44. System.out.println("花费的时间为:" + (end - start));//20000:83065
  45. } catch (Exception e) {
  46. e.printStackTrace();
  47. }finally{
  48. JDBCUtils.closeResource(conn, ps);
  49. }
  50. }
  51. /*
  52. * 批量插入的方式三:
  53. * 1.addBatch()、executeBatch()、clearBatch()
  54. * 2.mysql服务器默认是关闭批处理的,我们需要通过一个参数,让mysql开启批处理的支持。
  55. * ?rewriteBatchedStatements=true 写在配置文件的url后面
  56. * 3.使用更新的mysql 驱动:mysql-connector-java-5.1.37-bin.jar
  57. */
  58. @Test
  59. public void testInsert2() {
  60. Connection conn = null;
  61. PreparedStatement ps = null;
  62. try {
  63. long start = System.currentTimeMillis();
  64. conn = JDBCUtils.getConnection();
  65. String sql = "insert into goods(name)values(?)";
  66. ps = conn.prepareStatement(sql);
  67. for(int i = 1;i <= 1000000;i++){
  68. ps.setObject(1, "name_" + i);
  69. //1."攒"sql
  70. ps.addBatch();
  71. if(i % 500 == 0){
  72. //2.执行batch
  73. ps.executeBatch();
  74. //3.清空batch
  75. ps.clearBatch();
  76. }
  77. }
  78. long end = System.currentTimeMillis();
  79. System.out.println("花费的时间为:" + (end - start));//20000:83065 -- 565
  80. } catch (Exception e) { //1000000:16086
  81. e.printStackTrace();
  82. }finally{
  83. JDBCUtils.closeResource(conn, ps);
  84. }
  85. }
  86. //批量插入的方式四:设置连接不允许自动提交数据
  87. @Test
  88. public void testInsert3() {
  89. Connection conn = null;
  90. PreparedStatement ps = null;
  91. try {
  92. long start = System.currentTimeMillis();
  93. conn = JDBCUtils.getConnection();
  94. //设置不允许自动提交数据
  95. conn.setAutoCommit(false);
  96. String sql = "insert into goods(name)values(?)";
  97. ps = conn.prepareStatement(sql);
  98. for(int i = 1;i <= 1000000;i++){
  99. ps.setObject(1, "name_" + i);
  100. //1."攒"sql
  101. ps.addBatch();
  102. if(i % 500 == 0){
  103. //2.执行batch
  104. ps.executeBatch();
  105. //3.清空batch
  106. ps.clearBatch();
  107. }
  108. }
  109. //提交数据
  110. conn.commit();
  111. long end = System.currentTimeMillis();
  112. System.out.println("花费的时间为:" + (end - start));//20000:83065 -- 565
  113. } catch (Exception e) { //1000000:16086 -- 5114
  114. e.printStackTrace();
  115. }finally{
  116. JDBCUtils.closeResource(conn, ps);
  117. }
  118. }
  119. }