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
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.
Using the Apache DbUtils library you can easily return a ResultSet as a List of Maps.
public List query(String query) {
List result = null;
try {
QueryRunner qrun = new QueryRunner();
result = (List) qrun.query(connection, query, new MapListHandler());
} catch (Exception ex) {
ex.printStackTrace();
}
return result;
}
Put the code in a method. It's very simple to call methods...
Off the top of my head:
public static List<Integer> readInts(
PreparedStatement statement
) throws SQLException {
ResultSet results = statement.executeQuery();
try {
assert results.getMetaData().getColumnCount() == 1;
List<Integer> ints = new ArrayList<Integer>();
while (results.next()) {
ints.add(Integer.valueOf(results.getInt(1)));
}
return ints;
} finally {
results.close();
}
}
Then just call it as:
List<Integer> ids = readInts(myStatemnet);
Done.