Empty Resultset

How do I check for an empty resultset? I need to know if it contains any records before I try to read the fields and cause an error.

[139 byte] By [SLDykea] at [2007-11-27 10:44:21]
# 1

read them in a loop

ResultSet rs = ps.executeSql();

while(rs.next())

{

//do your reading here

}

~Tim

SomeoneElsea at 2007-7-28 20:06:21 > top of Java-index,Java Essentials,New To Java...
# 2

> How do I check for an empty resultset?

if (!rs.next())

But you don't need to do that.

> I need to know

> if it contains any records before I try to read the

> fields and cause an error.

No, you don't.

while (rs.next()) {

String name = rs.getString("name");

// etc.

}

If it's empty, you'll never enter the body of the while loop.

Or, if it's one where you know there will only be one or zero rows, then use if instead of while.

jverda at 2007-7-28 20:06:21 > top of Java-index,Java Essentials,New To Java...