2

hello fellow java developers.

I'm having a bit of an issue here. I have code that gets a resultset from an oracle database, prints each row to a file, then gets the next row - and continues till the end of the resultset.

Only this isn't what happens. What happens is that it gets the resultset, starts iterating through the rows, printing to file as it goes, until it runs out of memory - claiming it needs more space on the java heap.

The app is currently running with 2g of memory on the heap and the code breaks at about the 150000th row.

I'm using jodbc6.jar and java 6

Here is an idea of what my code is doing:

Connection conn = DriverManager.getConnection(url,"name","pwd");

conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery(strSql);

String strVar_1 = null;

long lCount = 0;
while(rset.next()){
        lCount++;
        if (lCount % 100000 == 0){
            System.out.println(lCount + " rows completed");
        }
      strVar_1 = rset.getString("StringID");  /// breaks here!!!!!!!!!

      if (strVar_1 == null){
        strVar_1 = "";
      }         
      if (!strQuery_1.equals("")){
        out.write(strVar_1 + "\n");
      }
 }
 out.close();

2 Answers 2

4

Try below:

Statement stmt = conn.createStatement();
stmt.setFetchSize(someInt);
ResultSet rset = stmt.executeQuery(strSql);

This will control how many records are fetched at a time.

Sign up to request clarification or add additional context in comments.

8 Comments

Ok, will try it and report back (query takes a while to return records from across the network) thanks
I figured your query was returning quite a large number of rows. Just as a side note, you should also call close on the ResultSet, Statement, and Connection.
I think its working, its just a little weird... the result is that when my count is written to the screen down to X00,000. all of the counts clear, and begin again starting at 100,000. Very odd behaviour... Its like its running the process over and over again. Do I need to add anything special to my while clause? Like, getnextfetchset or something?
No, it should be transparent.
What does your actual SQL query look like?
|
0

Well maybe another way of dealing with such large data is to keep returning the row and writing it to a file. This way the string buffer does not keep growing and you should be able to write all the records to file system and then read them late

1 Comment

Thanks - and I do the out.write(strVar_1 + "\n"); is me writing the data to file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.