Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

how to count exactly the rows of a ResulSet

Thread view: 
Mariano - 26 Jun 2007 20:28 GMT
Then, i have this need, i use this method, it seems works fine, but
for this method there are no differences if the resulSet contain one
row or if resultSet is empty, method return always 1 to me. For
resultSet larger than one row it works perfectly.
This is my semi-functionant code:

public int rowCount(ResultSet r) throws SQLException {
       r.last();
       int rc = r.getRow();
       r.beforeFirst();
       return rc;
   }

How can I modify it, to have rc=0 if rs is empty, rc=1 if there is one
row in rs, and so on????
Thank u to all...
Regards
Martin Gregorie - 27 Jun 2007 00:11 GMT
> Then, i have this need, i use this method, it seems works fine, but
> for this method there are no differences if the resulSet contain one
[quoted text clipped - 13 lines]
> Thank u to all...
> Regards

last() returns a boolean. Use it.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

bencoe@gmail.com - 27 Jun 2007 05:03 GMT
On Jun 26, 7:11 pm, Martin Gregorie <mar...@see.sig.for.address>
wrote:
> > Then, i have this need, i use this method, it seems works fine, but
> > for this method there are no differences if the resulSet contain one
[quoted text clipped - 20 lines]
> gregorie. | Essex, UK
> org       |

I was just looking through the docs,

isn't r.getFetchSize() what you're looking for?

--------
public int getFetchSize()
                throws SQLException

   Retrieves the fetch size for this ResultSet object.

   Returns:
       the current fetch size for this ResultSet object

--------
Ben
http://www.plink-search.com
Mariano - 27 Jun 2007 08:36 GMT
On 27 Giu, 06:03, ben...@gmail.com wrote:
> On Jun 26, 7:11 pm, Martin Gregorie <mar...@see.sig.for.address>
> wrote:
[quoted text clipped - 39 lines]
> --------
> Benhttp://www.plink-search.com

using getFetchSize() the result is always 1. Someone help me
pleaseeeeee
Martin Gregorie - 27 Jun 2007 12:44 GMT
> On 27 Giu, 06:03, ben...@gmail.com wrote:
>> On Jun 26, 7:11 pm, Martin Gregorie <mar...@see.sig.for.address>
[quoted text clipped - 38 lines]
> using getFetchSize() the result is always 1. Someone help me
> pleaseeeeee

I already told you an answer which should work with only minor changes
to your current code. If you ignore return values from methods such as
last() then of course your code won't work.

Hint: last() returns false if the result set is empty.

A quick look at the ResultSet documentation shows that getFetchSize()
(the opposite of setFetchSize() BTW - there's a clue!) shows how many
rows are retrieved from the database at a time, not how many rows there
are in the result set. getResultSet() is NOT the answer to your problem.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |

Lew - 29 Jun 2007 06:32 GMT
> using getFetchSize() the result is always 1. Someone help me
> pleaseeeeee

How is one supposed to pronounce the extension of the silent "e"?

Or is that simply a typographic convention for really annoying whining?

Signature

Lew

Joshua Cranmer - 29 Jun 2007 21:57 GMT
>> using getFetchSize() the result is always 1. Someone help me
>> pleaseeeeee
>
> How is one supposed to pronounce the extension of the silent "e"?
>
> Or is that simply a typographic convention for really annoying whining?

No, whining is supposed to be:
  E   E
PLE E E E EASE!
    E   E
(Where the extra E's are supposed to be set slightly above/below the
line. It's hard to do in a terminal).
JT - 27 Jun 2007 10:25 GMT
> Then, i have this need, i use this method, it seems works fine, but
> for this method there are no differences if the resulSet contain one
[quoted text clipped - 13 lines]
> Thank u to all...
> Regards

I'm  not sure if this is possible, but why couldn't you execute a
prepared statement that says "select count(*) from some_table" and have
the number it brings back stored as your rc?
jassi mahay - 30 Jun 2007 05:56 GMT
in JDBC 2.0 , we can count rows in ResultSet rs ;

int count = rs.getRow();

without select count(*) or scanning all rows.

or still better get rs into Vector vect and use easily
rows = new Vector();
while (resultSet.next())
           {
               Vector newRow = new Vector();
               for (int i = 1; i <= getColumnCount(); i++)
               {
               newRow.addElement(resultSet.getObject(i));
               }
               rows.addElement(newRow);
           }
get this vector returned and use freely, amny methods are htere..
vector.size();

Thanks and  Regards

JS Mahay
Feel Ethereal..
Martin Gregorie - 30 Jun 2007 19:46 GMT
> in JDBC 2.0 , we can count rows in ResultSet rs ;
>
> int count = rs.getRow();

getRow() returns the current row number, *not* the number of rows in the
result set. That's true for JDK 1.4 and I find it hard to imagine that
its function would be different in any other version of Java.

The OP is more correct that you. At least he tried to make the last row
current before invoking getRow().

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.