Skip to content
Advertisement

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());
        }

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement