How can I retrieve a JDBC ResultSet as an ArrayList?

前端 未结 3 1792
有刺的猬
有刺的猬 2020-12-25 15:40

I\'m doing a query to retrieve a large amount of IDs (integers). Instead of iterating millions of times through the ResultSet and copying everything one-by-one to an ArrayLi

3条回答
  •  鱼传尺愫
    2020-12-25 15:52

    If your problem is poor performance, tune the statement before executing it with

    java.sql.Statement.setFetchSize(int)
    

    Experiment with 100, 1000, 10000,.. This will avoid unnecessary roundtrips, which may be the cause of the slowness you mentioned.

    Also, ArrayList.add() may be slow if it must resize the internal array many times, as it creates a new array and copies all data to there. Try LinkedList instead.

提交回复
热议问题