Search code examples
javaoraclecursorfetchresultset

JAVA Getting data from Database Without a loop


I am Programming a software with JAVA and using the Oracle DB.

Normally we obtain the values from the Database using a Loop like

Resultset rt = (Resultset) cs.getObject(1);

while(rt.next){
    ....
}

But it sound is more slowly when fetch thousand of data from the database.

My question is:

In Oracle DB: I created a Procedure like this and it is the Iterating data and assign to the cursor.

Ex.procedure test_pro(sysref_cursor out info) as

open info select * from user_tbl ......

end test_pro;

In JAVA Code: As I mentioned before I Iterate a the resultset for obtain values, but the side of database, even I select the values, why should I use a loop for getting that values?

(another fact in the .net frameworks, there are using the database binding concept. So is any way in the java, binding the database procedures like .net 's, without the iterating. )


Solution

  • Depending on what you are going to do with that data and at which frequence, the choice for a ref_cursor might be a good or a bad one. Ref_cursors are intended to give non Oracle aware programs a way to pass it data, for reporting purposes.

    In you case, stick to the looping but don't forget to implement array fetching because this has a tremendous effect on the performance. The database passes blocks of rows to your jdbc buffer at the client and your code fetches rows from that buffer. By the time you hit the end of the buffer, the Jdbc layer requests the next chunk of rows from the database, eliminating lot's of network round trips. The default already fetches 10 rows at a time. For larger sets, use bigger numbers, if memory can provide the room.

    See Oracle® Database JDBC Developer's Guide and Reference