千家信息网

java 并发插入数据到oracle

发表于:2025-01-21 作者:千家信息网编辑
千家信息网最后更新 2025年01月21日,各位技术大牛,请教java 多线程问题由于项目需要,需要从oracle中dept将表记录迁移到dept2中,dept和dept2表结构一样。设想:查询:Statement stmt = conn.cr
千家信息网最后更新 2025年01月21日java 并发插入数据到oracle

各位技术大牛,请教java 多线程问题

由于项目需要,需要从oracle中dept将表记录迁移到dept2中,dept和dept2表结构一样。
设想:

查询:
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

ResultSet rs = stmt.executeQuery("select * from dept");

通过 rs.last();得到记录的总个数,然后通过这个总个数,多线程实现迁移所有的记录。

插入:
PreparedStatement pstmt = null;
String psql = "insert into dept2 values(?,?,?)";
pstmt = conn.prepareStatement(psql);

其中的index为rs结果集的索引值,当index=1,表示rs.next()的第一个值
rs.absolute(index);
pstmt.setInt(1, rs.getInt(1));
pstmt.setString(2, rs.getString(2));
pstmt.setString(3, rs.getString(3));

pstmt.addBatch();

本人对多线程不是很了解,多次尝试,没能实现多线程迁移数据,希望有高手指点。谢谢!

  1. public class ConcurrentTestPreparedStmt

  2. {

  3. public static int totalRowNum;

  4. public static int index = 1;

  5. public static boolean isFinish;

  6. public static void main(String[] args) throws Exception

  7. {

  8. Class.forName("oracle.jdbc.driver.OracleDriver");

  9. String url = "jdbc:oracle:thin:@192.168.5.201:1521:orcl";

  10. String user = "scott";

  11. String password = "tiger";

  12. String sql = "select * from dept";

  13. Connection conn = DriverManager.getConnection(url, user, password);

  14. /**

  15. * 读取

  16. */

  17. Statement stmt = conn.createStatement(

  18. ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

  19. ResultSet rs = stmt.executeQuery(sql);

  20. /**

  21. * 写入

  22. * 使用批处理

  23. */

  24. PreparedStatement pstmt = null;

  25. String psql = "insert into testpstmt values(?,?,?)";

  26. pstmt = conn.prepareStatement(psql);

  27. //rs.next 只要为true,说明表中记录数

  28. rs.last();

  29. totalRowNum = rs.getRow();

  30. int i = 0;

  31. //问题所在,当 i<2时,表示两个线程,执行时,往目的表中插入了同样一行记录。

  32. while (i < 1 && !isFinish)

  33. {

  34. TaskThread t = new ConcurrentTestPreparedStmt().new TaskThread(

  35. pstmt, rs);

  36. new Thread(t).start();

  37. i++;

  38. }

  39. //如果没完成,则主线程等待

  40. while (!isFinish)

  41. {

  42. Thread.sleep(500);

  43. }

  44. pstmt.executeBatch();

  45. conn.commit();

  46. System.out.println("SUCCESS");

  47. }

  48. public synchronized int incrementIndex()

  49. {

  50. return index++;

  51. }

  52. public synchronized int getExecuteNum()

  53. {

  54. return index;

  55. }

  56. /**

  57. * 并发迁移类

  58. * @author ttan

  59. */

  60. class TaskThread implements Runnable

  61. {

  62. PreparedStatement pstmt = null;

  63. ResultSet rs = null;

  64. public TaskThread(PreparedStatement pstmt, ResultSet rs)

  65. {

  66. this.rs = rs;

  67. this.pstmt = pstmt;

  68. }

  69. @Override

  70. public void run()

  71. {

  72. int index = incrementIndex();

  73. //指定到具体的行数

  74. try

  75. {

  76. while (index <= totalRowNum)

  77. {

  78. System.out.println("index = " + index);

  79. rs.absolute(index);

  80. pstmt.setInt(1, rs.getInt(1));

  81. pstmt.setString(2, rs.getString(2));

  82. pstmt.setString(3, rs.getString(3));

  83. pstmt.addBatch();

  84. index = incrementIndex();

  85. }

  86. if (getExecuteNum() > totalRowNum)

  87. {

  88. isFinish = true;

  89. return;

  90. }

  91. }

  92. catch (SQLException e)

  93. {

  94. e.printStackTrace();

  95. }

  96. }

  97. }

  98. }

java企业级通用权限安全框架源码获取【下载地址】

0