Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Behaviour of ResultSet

 
Abhishk Singh
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On reaching statement 2 in:
Statement 1;
resultset = prepstmt.executeQuery();
Statement 2;
,can we assume that all the required data has been fetched ?
I mean to ask that when we reach the statement: while(resultset.next()) ,is it the case that all the data has been fetched and filled in the resultset or the resultset keeps on getting updated when the while loop is running ?
 
Jan Cumps
Bartender
Posts: 2588
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Abhishek , Welcome to JavaRanch.

No you can't assume that all the required data has been fetched . You can't think of a ResultSet as a container that holds your data.
After you execute your query, the ResultSet serves as a gateway to retrieve that resultsfrom the database.
That is what you do with while (ResultSet.next()):
You loop over the rows returned by the SQL statement, and the ResultSet serves these rows one by one.

The resultset is not updated during the while (next()) loop, but the record pointer is moved to the next row.

(note and disclaimer: simplified explanation. We don't know how a JDBC driver actualy fulfills this functionality)
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jan Cumps wrote:Hi Abhishek , Welcome to JavaRanch.

No you can't assume that all the required data has been fetched . You can't think of a ResultSet as a container that holds your data.
After you execute your query, the ResultSet serves as a gateway to retrieve that resultsfrom the database.


Nice explanation Jan.

 
Ravi Shankar Kumar
Ranch Hand
Posts: 30
1
Java MyEclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes jan is 100% right resultset is like a pointer
 
Abhishk Singh
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone,thanks for the info.
one more querstion: could you tell something about usage and effects of resultset.setFetchSize(); ?
what is referred to by FetchSize?
 
Avishkar Nikale
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
abhishek singhania wrote:
could you tell something about usage and effects of resultset.setFetchSize(); ?
what is referred to by FetchSize?


Hi Abhishek,

Refer to the JDBC API for more details about what the spec demands/assures.

http://download.oracle.com/javase/6/docs/api/java/sql/ResultSet.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic