469,323 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

JAVA ResultSet Problem?

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
5 10388
-----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
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
-----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
-----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
-----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.

Similar topics

reply views Thread by hiisikukko | last post: by
reply views Thread by anaik100 | last post: by
oll3i
reply views Thread by oll3i | last post: by
madhoriya22
21 posts views Thread by madhoriya22 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.