Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / February 2004

Tip: Looking for answers? Try searching our database.

JAVA ResultSet Problem?

Thread view: 
kaiwing18@hotmail.com - 26 Feb 2004 14:47 GMT
Hi ,

Please see this code:

ResultSet rs=course.showStudent();

while(rs.next()) {
out.println(rs.getString("name"));
}

How can i know whether rs will have records before i print it out? As
i want to print some messages when no record is found.

Best regards,

Ricky
Tim Ashman - 26 Feb 2004 15:05 GMT
The while will fail if there are no records.

> Hi ,
>
[quoted text clipped - 12 lines]
>
> Ricky
Bjorn Abelli - 26 Feb 2004 15:24 GMT
"Tim Ashman" told something not quite accurate...

> The while will fail if there are no records.

No, it will not "fail" just because the query didn't retrieve any records.
It will just not enter the body of the loop.

Look at the documentation of next() for ResultSet in the API.

[snip]

> > ResultSet rs = course.showStudent();
> >
[quoted text clipped - 4 lines]
> > How can i know whether rs will have records
> > before i print it out?

You don't, or rather "next" will tell you.

If there's no records, rs.next() will return false at the first try, and
will not enter the body of the loop.

> > As i want to print some messages
> > when no record is found.

int numberOfRecords = 0;
while(rs.next())
{
  numberOfRecords++;
  out.println(rs.getString("name"));
}

if (numberOfRecords == 0)
{
   out.println("No records!");
}

// Bjorn A
Tim Ashman - 27 Feb 2004 04:39 GMT
> "Tim Ashman" told something not quite accurate...
>
[quoted text clipped - 4 lines]
>
> Look at the documentation of next() for ResultSet in the API.

Ok I misused the term fail.  I meant the condition will fail (false) and run
the while loop statements.

....
Oscar kind - 26 Feb 2004 17:22 GMT
> ResultSet rs=course.showStudent();

 boolean firstRecord = false;
> while(rs.next()) {
     if (!firstRecord) {
       firstRecord = true;
       // Code for the case that there are records.
     }
>     out.println(rs.getString("name"));
> }
 if {!firstRecord) {
   // Code for the case that there are no record.
 }

> How can i know whether rs will have records before i print it out? As
> i want to print some messages when no record is found.

How about above additions? I know it isn't pretty, but it works.
Another thing you might do is:

boolean hasRecords = rs.next();
if (hasRecords) {
   // Code for the case that there are records.
} else {
   // Code for the case that there are no record.
}
while (hasRecords) {
   out.println(rs.getString("name"));
   hasRecords = rs.next();
}

Which of course has the disadvantage that the assignment to hasRecords is
duplicated.

Oscar

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Java/J2EE Developer                             email available on website

kaeli - 26 Feb 2004 18:28 GMT
> Hi ,
>
[quoted text clipped - 8 lines]
> How can i know whether rs will have records before i print it out? As
> i want to print some messages when no record is found.

Advantage to a boolean to keep track of this: record count kept for any
other reason you might need it. I like to put the total at the bottom of
the display (I use JSP mostly, so I have html tables) a lot.

int c=0;
while (rs && rs.next())
  {
  c++;
  // more code to process records
  }
if (c == 0)
  {
  // no records
  }
Signature

--
~kaeli~
A man's home is his castle..., in a manor of speaking.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.