John Collins
John Collins

Reputation: 131

Out of memory java heap spcae with MySQL

I'm getting the above error when trying retrieve some data from a MySQL statement, as the error states the error is in line 118; here is the MySQL:

                    .prepareStatement("SELECT orderr.* FROM orderr JOIN person ON orderr.personID = person.personID WHERE person.first_name = ?",java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY, Integer.MIN_VALUE);

Code near line 118:

            int index = 0;
        if (rs.next()) {
            index++;
            rs.beforeFirst();
            while (rs.next()) {
                for (int i = 1; i < 4; i++) { // retrive column data from order
                    message.append(rs.getInt(i));   //table and append to sb.
                    if(i != 3)message.append(" "); <---line 118.
                }
                if(rs.next()){
                    rs.absolute(index);
                    message.append("\n");
                }
            }
        } 

The exact error I get is:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2367)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:130)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:114)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:415)
at java.lang.StringBuffer.append(StringBuffer.java:237)
at stl.Sql.printPersonsOrders(Sql.java:118)
at stl.Main.main(Main.java:19)

This error only occurs when I have added the same data twice to the table and then try and retrieve, the amount of data should only be one or two rows.

Upvotes: 0

Views: 239

Answers (1)

Markus Malkusch
Markus Malkusch

Reputation: 7868

Your rs.absolute(index) moves the cursor back (to row 1) and the while(rs.next()) loop never ends. This is no problem for results sets with one row, as the result set offset is 1.

Upvotes: 1

Related Questions