• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Check the result set

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I want to know what is the difference between while(rs.next()) and if(rs.next())? I'm not sure about this but I think that while is for more than 1 data in the db while if is to check whether there is data or not inside db and can only retrieve 1 data.
so my question is:
if I have no data on 1 thing but I have 1 or more data on another 1 then how to check for that one.because in my program some still don't have data so this part, I always have java.lang.nullpointerexception. thanks...
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The next() method of a ResultSet moves the cursor on to the next record, returning true if there is another record to move to and false if there is not. Typically since you don't usually know how many records are in a ResultSet you would access its contents in a loop, so:

is the typical way of dealing with ResultSets. I suppose you could use:

if you are sure there will always be 0 or 1 records. But it sounds like a dangerous assumption.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While you want to iterate through the entire ResultSet you have to use


There might be some instances where you want to just check if there are results in the ResultSet, so instead of iterating through the entire ResultSet we can use
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic