How to improve performance with executeBatch?

后端 未结 1 511
野的像风
野的像风 2021-02-08 00:05

I am writing Java code for inserting 45000 records into the table and I\'m using the following code:

//create Db Connection
List sqlInsertQueries =         


        
1条回答
  •  盖世英雄少女心
    2021-02-08 00:30

    You should make sure that auto commit is false, and use prepared statements. Prepared statements can be used for insert and update.

    connection con.setAutoCommit(false);  
    PreparedStatement prepStmt = con.prepareStatement("INSERT INTO table (val1,val2) VALUES (?,?)");
    
    for all values:
      prepStmt.setString(1,val1);
      prepStmt.setString(2,val2);
      prepStmt.addBatch();    
    
    stmt.executeBatch(); 
    conn.commit(); 
    

    0 讨论(0)
提交回复
热议问题