By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,340 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

JAVA ResultSet Problem?

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

<ka*******@hotmail.com> ha scritto nel messaggio
news:d8**************************@posting.google.c om...

ResultSet rs=course.showStudent();
boolean bEmpty = true;
while(rs.next()) {
bEmpty = false;
out.println(rs.getString("name"));
}

if (bEmpty)
out.println("Sad course! There are no students!");

Best regards,
GianpieroP

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBQD5h3Z4sPKh9SaicEQKUUACg1XQ3ojg+lYe/7xx5hUsPKeiBhZcAoJMQ
YW1ErfKUsIqJrobIGFmxM+IG
=PR0S
-----END PGP SIGNATURE-----
Jul 17 '05 #2

P: n/a
nos

"GianpieroP" <gi**********************@fastwebnet.it> wrote in message
news:wp*****************@tornado.fastwebnet.it...
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

<ka*******@hotmail.com> ha scritto nel messaggio
news:d8**************************@posting.google.c om...

ResultSet rs=course.showStudent();
boolean bEmpty = true;
while(rs.next()) {
bEmpty = false;
out.println(rs.getString("name"));
}

if (bEmpty)
out.println("Sad course! There are no students!");

Best regards,
GianpieroP

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBQD5h3Z4sPKh9SaicEQKUUACg1XQ3ojg+lYe/7xx5hUsPKeiBhZcAoJMQ
YW1ErfKUsIqJrobIGFmxM+IG
=PR0S
-----END PGP SIGNATURE-----


if (rs.length == 0)
Jul 17 '05 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"nos" <no*@nospam.com> ha scritto nel messaggio
news:8Lu%b.67330$4o.87168@attbi_s52...

| > ResultSet rs=course.showStudent();

| if (rs.length == 0)

Sorry, but I can't find "length" field in the
"java.sql.ResultSet" interface documentation
(java api doc).

GianpieroP

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA+AwUBQD58r54sPKh9SaicEQKA7QCYzOUs7cXQ1fC32hDkh+ Zm1K7GHwCdEhU+
MDhowp04nAk4J6cqUwLd3gA=
=9F7j
-----END PGP SIGNATURE-----
Jul 17 '05 #4

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

GianpieroP wrote:
ResultSet rs=course.showStudent();
boolean bEmpty = true;
while(rs.next()) {
bEmpty = false;
out.println(rs.getString("name"));
}

if (bEmpty)
out.println("Sad course! There are no students!");

Best regards,
GianpieroP


Hey,
If you need to know this at the beginning rather than the end:

ResultSet rs = course.showStudent();
if (!rs.isBeforeFirst()) {
System.out.println("No students");
}
while (rs.next()) {
...
}

NOTE: This is untested (based on the JavaDoc specification that
isBeforeFirst() returns, and I quote, "true if the cursor is before
the first row; false if the cursor is at any other position or the
result set contains no rows".

- --
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAP+sjnwjA8LryK2IRApg+AJ94GM3CjVUQXnTFCXG8Ui YXUJpsnQCdFF0N
uTshC5VCpKcEvwUkIhWZ0n8=
=YNQo
-----END PGP SIGNATURE-----
Jul 17 '05 #5

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

"Chris" <ch*******@hotmail.com> ha scritto nel messaggio
news:lzU%b.23956$A12.5733@edtnps84...

| > while(rs.next()) {
| > bEmpty = false;
| > out.println(rs.getString("name"));
| > }
| >
| > if (bEmpty)
| > out.println("Sad course! There are no students!");
| >
| > Best regards,
| > GianpieroP
|
| Hey,
| If you need to know this at the beginning rather than the end:

It's the same thing: if there are not records, the while iteration
does
not write any text.

GianpieroP

-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0

iQA/AwUBQECu6Z4sPKh9SaicEQKt/wCfXZG1wZm0QTfLb2FjsTbVUDFABfQAnA8v
w+OMByFENnUbDGoaPxbx/jti
=0X4+
-----END PGP SIGNATURE-----
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.