ResultSet needs a rewind

ResultSet needs a rewind

Post by fb » Wed, 19 Apr 2006 06:08:51


I am looking for a way to determine in a ResultSet is empty, without
running through it. If no results were retrieved, I'd like to output
the text 'No matching products' (into a list box). For Example, the
following snippet does not work, unless I comment out the 'if' block:

NOTES:
myRSet is a ResultSet
viewList is a List
statusLabel is a Label

//Begin Snippet

if (myRSet.next() == false){
String noData = "No Data Found";
viewList.add(noData);
}

String myRowStr = "";
while (myRSet.next() == true) {
myRowStr = myRSet.getString("category_id");
statusLabel.setText("ID Valid. "+ myRowStr +"
Accepted.");
viewList.add("Category_ID = " +myRowStr);
}

I'm stumped...Any ideas?
ttyl.

fb
--
Pro'-gram 1) n. A magical spell cast over a computer which transforms
user input into error messages. 2) vt. An activity similar to banging
one's head against a wall, but with less opportunity for relief.
 
 
 

ResultSet needs a rewind

Post by Philipp Le » Wed, 19 Apr 2006 06:23:01

I guess the problem is that you are (after calling myRSet.next() in the
if part) already in the _second_ row of the result set, i.e. you already
pull one element from the result set when you'rte checking whether it's
empty. You could use the isLast() method instead.

/philipp

fb schrieb:

 
 
 

ResultSet needs a rewind

Post by fb » Wed, 19 Apr 2006 07:42:27


> fb schrieb:

> I guess the problem is that you are (after calling myRSet.next() in the
> if part) already in the _second_ row of the result set, i.e. you already
> pull one element from the result set when you'rte checking whether it's
> empty. You could use the isLast() method instead.
>
> /philipp
>
>

Interesting...thanks for the help...the only problem I get with that is
the following message:
'Result set type is TYPE_FORWARD_ONLY'
Is there a way around this?

ttyl.
 
 
 

ResultSet needs a rewind

Post by Patricia S » Wed, 19 Apr 2006 23:48:25


Here's one option:

**** DANGER! DANGER! UNTESTED CODE AHEAD! ****

if (myRSet.next()) {
do {
String myRowStr = myRSet.getString("category_id");
statusLabel.setText("ID Valid. "+ myRowStr +" Accepted.");
viewList.add("Category_ID = " +myRowStr);
} while (myRSet.next());
} else {
String noData = "No Data Found";
viewList.add(noData);
}

There are several other options. For example, you could have your while
loop count the rows. After the loop, test the counter and do the noData
stuff if it is zero.

Patricia
 
 
 

ResultSet needs a rewind

Post by Chris » Thu, 20 Apr 2006 04:04:08

The way I do it is simple as follows.


boolean check = false; //remains false if no results
while(res.next()){
check = true;
//Do whatever with the results if true
myRowStr = myRSet.getString("category_id");
statusLabel.setText("ID Valid. "+ myRowStr +" Accepted.");
viewList.add("Category_ID = " +myRowStr);

}

//this will only execute if no results
if(!check){
//The while loop never ran so
//no results.
String noData = "No Data Found";
viewList.add(noData);
}

HTH

Chris
 
 
 

ResultSet needs a rewind

Post by Chris » Thu, 20 Apr 2006 04:26:00


Statement state = connection.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);

that should do it.

Chris.
 
 
 

ResultSet needs a rewind

Post by Paul Hamak » Thu, 20 Apr 2006 16:00:46

Combined with
if ( ! rs.first() )
http://www.yqcomputer.com/
explains a couple of examples
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://www.yqcomputer.com/
 
 
 

ResultSet needs a rewind

Post by fb » Sat, 22 Apr 2006 16:15:52


<SNIP>
Thanks everyone!