Re: isLast() and empty ResultSet

From: "Ruediger Herrmann" <ruediger(dot)herrmann(at)gmx(dot)de>
To: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: isLast() and empty ResultSet
Date: 2005-04-22 08:45:56
Message-ID: 14442.1114159556@www16.gmx.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc


Thanks a lot to all of you for the replies.

I'm now using isBeforeFirst() to test for an empty
ResultSet, wich as far as my tests show seems to work
if it is the first thing done.

Regards
Rüdiger

--- Oliver Jowett <oliver(at)opencloud(dot)com> wrote:
> Ruediger Herrmann wrote:
> > Hello,
> >
> > I implemented an Iterator interface iterating over
> a ResultSet. Therefore
> > I rely on isLast() to implement the
> Iterator#hasNext() method. This works
> > fine unless the whole ResultSet is empty.
> > For empty RresultSets, isLast always returns true.
> > Stepping through the code I found the reason
> therefore. Line 544 of
> > AbstractJdbc2ResultSet says
> > if (rows_size == 0)
> > return false; // No rows.
> > At least to me this is suspicious as I would
> return the opposite. Might that
> > be a bug or is there any other reason to behave
> like this?
>
> It's not a bug, AFAIK. isLast() returns true if the
> resultset is
> positioned *on* the last row of the resultset. This
> means that if you
> have a 5-row resultset, isLast() is true when the
> 5th row is the
> currently active row (and you can retrieve data from
> that row at that
> point).
>
> For a 0-row resultset, we can never be on the last
> row as there are no
> rows at all.
>
> You could try something like this for your hasNext()
> condition:
>
> rs.isBeforeFirst() || (rs.getRow() != 0 &&
> !rs.isLast())
>
> (note that isBeforeFirst() returns false on an empty
> resultset, per javadoc)
>
> -O
>
> ---------------------------(end of
> broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to
> majordomo(at)postgresql(dot)org
>
>
>

--
+++ GMX - Die erste Adresse für Mail, Message, More +++

1 GB Mailbox bereits in GMX FreeMail http://www.gmx.net/de/go/mail

Browse pgsql-jdbc by date

  From Date Subject
Next Message Xavier Poinsard 2005-04-22 08:46:47 Re: What Hibernate Object ID generator is recommanded for
Previous Message Oliver Jowett 2005-04-22 08:12:37 Re: PreparedStatement.setString