Java JDBC - Multiple prepared statement bulk insert

后端 未结 2 912
生来不讨喜
生来不讨喜 2020-12-15 09:28

Using JDBC (Oracle) I need to insert about thousand rows into each of two tables. Something like this:

\"INSERT INTO TABLE_A (A_ID, A_NAME, A_LAST_NAME) VALU         


        
相关标签:
2条回答
  • 2020-12-15 09:39

    If I understand your problem correctly, you have a problem with NEXTVAL and CURRVAL since CURRVAL might change due to other DB use? If so, you can change your code to this order:

    currentNextVal = select NEXTVAL
    INSERT into table_a with currentNextVal as the id
    INSERT into table_b with the same currentNextVal 
    

    Did I understand your problem correctly?

    0 讨论(0)
  • 2020-12-15 10:01

    You can try

    PreparedStatement ps = conn.prepareStatement(insert, Statement.RETURN_GENERATED_KEYS);
    ...
    ps.executeBatch();
    

    then

    ResultSet rs = ps.getGeneratedKeys();
    ps = conn.prepareStatement("INSERT INTO TABLE_B (B_ID, B_DESCRIPTION) VALUES (?, ?)");
    
    for ( int counter =0;rs.next(); counter++ ) { 
      ps.setInt(1,rs.getInt(0));
      ps.setString(2, myCollection.get(counter).getDescription());
      ps.addBatch();
    }
    ...
    
    0 讨论(0)
提交回复
热议问题