当前位置:   article > 正文

如何快速向数据库加入10万条数据_数据库插入几万条数据

数据库插入几万条数据

1、程序连接数据库,使用c3p0线程池;

2、程序使用线程池,多线程编程;

3、采用Fork/Join框架线程池(工作窃取(work-stealing)算法),更高效的多线程编程算法。

直接贴代码,代码举例中,近用小规模数据模拟大数据下的数据库批量插入操作。

1、数据库连接池

  1. package com.example.jdbcConnection;
  2. import com.mchange.v2.c3p0.ComboPooledDataSource;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. /**
  10. * Created by Liuxd on 2018/8/19.
  11. */
  12. public class TestC3p0 {
  13. private static Connection conn;
  14. private static ComboPooledDataSource dataSource;
  15. static {
  16. try {
  17. //获得c3p0连接池对象
  18. dataSource = new ComboPooledDataSource();
  19. dataSource.setUser("root");
  20. dataSource.setPassword("root");
  21. dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/foo?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2b8&useSSL=false");
  22. dataSource.setDriverClass("com.mysql.jdbc.Driver");
  23. dataSource.setInitialPoolSize(2);//初始化池大小
  24. dataSource.setMaxIdleTime(30);//最大空闲时间
  25. dataSource.setMaxPoolSize(20);//最多连接数
  26. dataSource.setMinPoolSize(2);//最少连接数
  27. dataSource.setMaxStatements(50);//每次最多可以执行多少个批处理语句
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. /**
  33. * 查询
  34. */
  35. private static List<Object[]> query() {
  36. List<Object[]> list = new ArrayList<Object[]>();
  37. try {
  38. // 获取数据库连接
  39. conn = dataSource.getConnection();
  40. // 查询sql
  41. String sql = "select * from user";
  42. // 读取数据
  43. PreparedStatement preparedStatement = conn.prepareStatement(sql);
  44. //结果集
  45. ResultSet resultSet = preparedStatement.executeQuery();
  46. while (resultSet.next()) {
  47. int uid = resultSet.getInt("uid");
  48. String name = resultSet.getString("name");
  49. Integer age = resultSet.getInt("age");
  50. String phone = resultSet.getString("phone");
  51. String passwd = resultSet.getString("passwd");
  52. Object[] objects = new Object[]{uid, name, age, phone, passwd};
  53. list.add(objects);
  54. }
  55. resultSet.close();
  56. preparedStatement.close();
  57. //Connection连接对象归还数据库连接池
  58. conn.close();
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. return list;
  63. }
  64. /**
  65. * 新增
  66. */
  67. public static void add(String name, int age, String phone, String passwd) {
  68. try {
  69. // 获取数据库连接
  70. conn = dataSource.getConnection();
  71. String insertSql = "insert into `user` (`name`, `age`, `phone`, `passwd`) values(?,?,?,?)";
  72. PreparedStatement ps = conn.prepareStatement(insertSql);
  73. ps.setString(1, name);
  74. ps.setInt(2, age);
  75. ps.setString(3, phone);
  76. ps.setString(4, passwd);
  77. int row = ps.executeUpdate();
  78. System.out.println("新增结果: " + row);
  79. ps.close();
  80. //Connection连接对象归还数据库连接池
  81. conn.close();
  82. } catch (Exception e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. /**
  87. * 修改
  88. */
  89. private static void update(int uid, String name, int age, String phone, String passwd) {
  90. try {
  91. // 获取数据库连接
  92. conn = dataSource.getConnection();
  93. String updateSql = "UPDATE USER t SET t.name=? ,t.age=?,t.phone=?,t.passwd=? WHERE t.uid=?";
  94. PreparedStatement preparedStatement = conn.prepareStatement(updateSql);
  95. preparedStatement.setString(1, name);
  96. preparedStatement.setInt(2, age);
  97. preparedStatement.setString(3, phone);
  98. preparedStatement.setString(4, passwd);
  99. preparedStatement.setLong(5, uid);
  100. // 执行sql
  101. preparedStatement.executeUpdate();
  102. int row = preparedStatement.executeUpdate();
  103. System.out.println("修改结果: " + row);
  104. //Connection连接对象归还数据库连接池
  105. conn.close();
  106. preparedStatement.close();
  107. } catch (Exception e) {
  108. e.printStackTrace();
  109. }
  110. }
  111. /**
  112. * 删除
  113. */
  114. private static void deleteById(int uid) {
  115. try {
  116. // 获取数据库连接
  117. conn = dataSource.getConnection();
  118. String sql = "delete from USER where uid=?";
  119. PreparedStatement preparedStatement = conn.prepareStatement(sql);
  120. preparedStatement.setInt(1, uid);
  121. int row = preparedStatement.executeUpdate();
  122. System.out.println("删除结果: " + row);
  123. preparedStatement.close();
  124. //Connection连接对象归还数据库连接池
  125. conn.close();
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. public static void main(String[] args) {
  131. /**
  132. * 1、验证连接数
  133. */
  134. for (int i = 0; i < 10; i++) {
  135. Connection connection = null;
  136. try {
  137. connection = dataSource.getConnection();
  138. System.out.println(connection.toString());
  139. } catch (SQLException e) {
  140. e.printStackTrace();
  141. } finally {
  142. if (null != connection) {
  143. try {
  144. connection.close();
  145. } catch (SQLException e) {
  146. e.printStackTrace();
  147. }
  148. }
  149. }
  150. }
  151. /**
  152. * 2、查询
  153. */
  154. List<Object[]> list = query();
  155. if (null != list && list.size() > 0) {
  156. for (int i = 0; i < list.size(); i++) {
  157. Object[] objects = list.get(i);
  158. for (int j = 0; j < objects.length; j++) {
  159. System.out.print(objects[j] + " ");
  160. }
  161. System.out.println();
  162. }
  163. }
  164. /**
  165. * 3、新增
  166. */
  167. String name = "乐乐";
  168. int age = 17;
  169. String phone = "13800138001";
  170. String passwd = "admin123";
  171. add(name, age, phone, passwd);
  172. /**
  173. * 4、修改
  174. */
  175. update(12, name, age, phone, passwd);
  176. /**
  177. * 5、删除
  178. */
  179. deleteById(3);
  180. }
  181. }

2、Fork/Join框架类

  1. package com.example.jdbcConnection;
  2. import java.util.concurrent.ForkJoinPool;
  3. import java.util.concurrent.TimeUnit;
  4. /**
  5. * Created by Liuxd on 2018/8/23.
  6. */
  7. public class TestForkJoinPool {
  8. public static void main(String[] args) throws Exception {
  9. System.out.println("*****************************程序开始执行*****************************");
  10. // 创建线程池,包含Runtime.getRuntime().availableProcessors()返回值作为个数的并行线程的ForkJoinPool
  11. ForkJoinPool forkJoinPool = new ForkJoinPool();
  12. TestC3p0 testC3p0 = new TestC3p0();
  13. // 提交可拆分的Task任务
  14. forkJoinPool.submit(new MyTask(0, 1000,testC3p0));
  15. //阻塞当前线程直到 ForkJoinPool 中所有的任务都执行结束
  16. forkJoinPool.awaitTermination(2, TimeUnit.SECONDS);
  17. // 关闭线程池
  18. forkJoinPool.shutdown();
  19. System.out.println("*****************************程序执行结束*****************************");
  20. }
  21. }

 

3、Task执行类

  1. package com.example.jdbcConnection;
  2. import java.util.concurrent.RecursiveAction;
  3. /**
  4. *
  5. * reated by Liuxd on 2018/8/23.
  6. */
  7. class MyTask extends RecursiveAction {
  8. // 每个"小任务"最多执行保存20个数
  9. private static final int MAX = 20;
  10. private int start;
  11. private int end;
  12. private TestC3p0 testC3p0;
  13. MyTask(int start, int end,TestC3p0 testC3p0) {
  14. this.start = start;
  15. this.end = end;
  16. this.testC3p0=testC3p0;
  17. }
  18. @Override
  19. protected void compute() {
  20. //end-start的值小于MAX时候,开始执行
  21. if ((end - start) < MAX) {
  22. for (int i = start; i < end; i++) {
  23. String name = "乐乐"+i;
  24. int age = 17+i;
  25. String phone = "1380013800"+i;
  26. String passwd = "admin123"+i;
  27. testC3p0.add(name, age, phone, passwd);
  28. System.out.println(Thread.currentThread().getName() + "保存"+name+" "+age+" "+" "+phone+" "+passwd);
  29. }
  30. } else {
  31. // 将大任务分解成两个小任务
  32. int middle = (start + end) / 2;
  33. MyTask left = new MyTask(start, middle,testC3p0);
  34. MyTask right = new MyTask(middle, end,testC3p0);
  35. // 并行执行两个小任务
  36. left.fork();
  37. right.fork();
  38. }
  39. }
  40. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/1019012
推荐阅读
相关标签
  

闽ICP备14008679号