I am getting this error:
Exception in thread \"main\" java.lang.OutOfMemoryError: Java heap space
at com.mysql.jdbc.MysqlIO.nextRowFast(MysqlIO.java:1585
Must the whole result from the query be in memory? If yes then you should buy more RAM. If not, then partitionning the problem will be the proper solution. You can even let the database consolidate the data for you in many cases.
If your solution to a given problem doesn't scale well with the amount of data present you will return to this problem even by extending your RAM. So the really good solution is to avoid the situation at all.