• 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

Iterating through resultset

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi,

I have executed an SQL query on the primary key of the table.So either no record will be returned or only one record will be returned.So do I need to iterate through the resultset by Iterator interface ? Is there any better way to access the resultset having only one record ?

Thanks,
Arka
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought you used while(rs,next()) ... and I thought you can't get an Iterator for a ResultSet.

But you should find the details in the Java Tutorials.
 
Arka Sharma
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Actually I'm using Hibernate.And I'm doing like

Iterator iterator = query.list();

And the HQL query is like " from Operator where OperatorId = :OperatorId" now this OperatorId is the primary key for Operator.So do I need to get the iterator and iterate through the list since only either one or no record will be returned by that query.

 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You ought to have told the full details; it is very different to use Hibernate, which I don't know about.
I couldn't find a list() method in the API index matching yours. It does seem peculiar that a method called list() returns an Iterator, rather than a "list". If "list" means java.util.List, that is an Iterable and you can get an Iterator for that.
ResultSet doesn't seem to have an isEmpty() method, but you can try declaring a boolean isEmpty and you can set isEmpty to false inside the while (rs.next())... loop.
 
reply
    Bookmark Topic Watch Topic
  • New Topic