Based on the comments, you create a Statement and ResultSet inside the loop, but do not close them. You need to also close them in a loop. This will free up internal resources.
In addition, you do not use the database cache of a prepared statement. Right now, you are binding a string to a parameter in an SQL string that causes the creation of 2M String objects instead of 1 String object. Better prepare the instructions before the cycle.
try { // ... statement = connection.prepareStatement("select NAME from TABLE_NAME where FIELD=?"); for ( /* 2M times? */ ) { statement.setInt(1, id); try { resultSet = statement.executeQuery(); // ... } finally { if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {} } } } finally { if (statement != null) try { statement.close(); } catch (SQLException ignore) {} }
Alternatively, you can also use the IN clause instead. For instance.
WHERE field IN (1,2,3,4,5);
This, however, is more complicated with placeholders. See Also: What is the best approach using JDBC to parameterize an IN clause?
Or as a completely different alternative, if necessary, with the help of a more experienced ninja DB / SQL administrator, rewrite the whole thing to get exactly the results you need, with just one SQL query. Ask, if necessary, a separate question about what's on sql here on SO.
source share