Iterating over ResultSet and adding its value in an ArrayList

前端 未结 2 1885
醉梦人生
醉梦人生 2020-12-08 15:18

I am iterating over an ResultSet and trying to copy its values in an ArrayList. The problem is that its traversing only once. But using resul

2条回答
  •  攒了一身酷
    2020-12-08 16:05

    Just for the fun, I'm offering an alternative solution using jOOQ and Java 8. Instead of using jOOQ, you could be using any other API that maps JDBC ResultSet to List, such as Spring JDBC or Apache DbUtils, or write your own ResultSetIterator:

    jOOQ 3.8 or less

    List list =
    DSL.using(connection)
       .fetch("SELECT col1, col2, col3, ...")
       .stream()
       .flatMap(r -> Arrays.stream(r.intoArray()))
       .collect(Collectors.toList());
    
    
    

    jOOQ 3.9

    List list =
    DSL.using(connection)
       .fetch("SELECT col1, col2, col3, ...")
       .stream()
       .flatMap(Record::intoStream)
       .collect(Collectors.toList());
    
    
    

    (Disclaimer, I work for the company behind jOOQ)

    提交回复
    热议问题