ResultSet in JDBC
1 posts in topic
Flat View  Flat View

Posted By:   munish_katyal
Posted On:   Sunday, March 16, 2003 06:03 AM

Why is it necessary to give rs.next() (rs is object of ResultSet)
when i have used this to select records from database.
to retrieve even first record i have to first use rs.next()

My another question is that if select statement returns nothing then what value rs will hold.
in this will rs.next() will give error.

Re: ResultSet in JDBC

Posted By:   Anonymous  
Posted On:   Sunday, March 16, 2003 12:36 PM

The ResultSet uses a pointer variable which points to the current row. This pointer is set before the first row after your query. Thats why you have to call rs.next() at least one time before retrieving any data from the result set.

If no row was returned by your query, the first call to rs.next() will return false, it won't throw an exception.

The following code fragment shows you, how to iterate through a result set:

ResultSet rs = statement.excuteQuery();

while(rs.next()) {
// copy data from rs


If you are interested in the first row, you can use if() instead of while().

About | Sitemap | Contact