BatchInsert.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package qh.lqg.utils.db;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.List;
  7. import java.util.TimeZone;
  8. import com.mysql.jdbc.Connection;
  9. import com.mysql.jdbc.PreparedStatement;
  10. public class BatchInsert {
  11. public void oneJdbcBatch(List<String> domain) throws SQLException,
  12. ClassNotFoundException {
  13. // 数据库连接驱动
  14. Class.forName("com.mysql.jdbc.Driver");
  15. // 连接数据库字符串
  16. Connection con = (Connection) DriverManager.getConnection(
  17. "jdbc:mysql://" + "localhost:3306/db_test", "lyq",
  18. "123456");
  19. // 关闭事务自动提交
  20. con.setAutoCommit(false);
  21. // 设置时间格式
  22. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
  23. // 设置时区,中国上海
  24. TimeZone t = sdf.getTimeZone();
  25. t.setRawOffset(0);
  26. sdf.setTimeZone(t);
  27. // 获得当前时间,单位为毫秒。
  28. // 计算公式:1秒1000毫秒,1分60秒,1时60分,1天24时。
  29. Long startTime = System.currentTimeMillis();
  30. // com.mysql.jdbc.PreparedStatement 数据库字符串构建类
  31. PreparedStatement pst = (PreparedStatement) con
  32. .prepareStatement("UPDATE `tw_domain` SET( `domain_name`) VALUES (?);");
  33. for (int i = 0; i < domain.size(); i++) {
  34. pst.setString(1, domain.get(i));// 设置第一个问号用i代替
  35. // 把一个SQL命令加入命令列表
  36. pst.addBatch();// mysql批处理函数
  37. }
  38. // 这样一个批处理命令就拼接完成
  39. // 可以打印出来看看
  40. // System.out.println(pst.toString());
  41. // 执行批量更新
  42. pst.executeBatch();
  43. // 语句执行完毕,提交本事务
  44. con.commit();
  45. Long endTime = System.currentTimeMillis();
  46. System.out.println("------一次插入30000条数据,最后提交命令情况-----");
  47. System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));
  48. pst.close();
  49. con.close();
  50. }
  51. public void muchJdbcBatch() throws ClassNotFoundException, SQLException {
  52. // 数据库连接驱动
  53. Class.forName("com.mysql.jdbc.Driver");
  54. // 连接数据库字符串
  55. Connection con = (Connection) DriverManager.getConnection(
  56. "jdbc:mysql://" + "localhost:3306/db_test", "root", "123456");
  57. // 关闭事务自动提交
  58. con.setAutoCommit(false);
  59. // 设置时间格式
  60. SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss:SS");
  61. // 设置时区,中国上海
  62. TimeZone t = sdf.getTimeZone();
  63. t.setRawOffset(0);
  64. sdf.setTimeZone(t);
  65. // 获得当前时间,单位为毫秒。
  66. // 计算公式:1秒1000毫秒,1分60秒,1时60分,1天24时。
  67. Long startTime = System.currentTimeMillis();
  68. // com.mysql.jdbc.PreparedStatement 数据库字符串构建类
  69. PreparedStatement pst = (PreparedStatement) con
  70. .prepareStatement("insert into jpa_person values (?,'中国')");
  71. for (int j = 0; j < 300; j++) {
  72. for (int i = 0; i < 100; i++) {
  73. pst.setInt(1, i + j);// 设置第一个问号用i代替
  74. // 把一个SQL命令加入命令列表
  75. pst.addBatch();// mysql批处理函数
  76. }
  77. pst.executeBatch();
  78. con.commit();
  79. }
  80. Long endTime = System.currentTimeMillis();
  81. System.out.println("------一次插入100条数据,然后提交,如此重复300次情况-----");
  82. System.out.println("用时:" + sdf.format(new Date(endTime - startTime)));
  83. pst.close();
  84. con.close();
  85. }
  86. public void oneJpaBatch() {
  87. }
  88. public void muchJpaBatch() {
  89. }
  90. }