'Latency in Iterator or foreach

i have one ArrayList that has a 1000 Insert SQL statement. but rather in execution time the latency of Iterator or (enhanced for loop) for this ArrayList take 1 minute . and my JFrame is not responding in this period. what can i do? Thanks

 Iterator itr = stms.iterator();
        while (itr.hasNext()) {                

            DB_STM.executeUpdate((String) itr.next());
        }


Solution 1:[1]

foreach internally uses iterator only, in order to handle bulk updates and inserts from JDBC, you can use batch updates using addBatch() and executeBatch() of PreparedStatement as shown here

This issue just related to internal latency of loops. Are you agree with me ?

No, I don't agree. Basically, there are few design issues with your code.

(1) You should handle bulk & costly (in terms of time) operations in the background (like using SwingWorker as others suggested).

(2) Use JDBC addBatch() and executeBatch() to reduce the database hits & improves the performance.

Solution 2:[2]

You should not run long operations in Swing AWT Event Thread. Use SwingWorker for this. https://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glorfindel
Solution 2 brummfondel