Solved

getting ResultSet row count

Posted on 2001-09-06
11
868 Views
Last Modified: 2007-11-27
Does anybody know how to retrieve the number of rows in a ResultSet before you start processing it.  I know that if the ResultSet is scrollable, you can move to the last row, use getRow() and move back.  The problem is that the ResultSet i'm dealing with is type forward only.
0
Comment
Question by:steeks
11 Comments
 
LVL 4

Expert Comment

by:pagladasu
ID: 6463143
Assuming that you are using JDBC 2.0, and there is a resulset called 'rs' created in your program. You can try  this:
int nrows=0;
if(rs.last()){
  nrows=rs.getRow();
}

nrows will contain the number of rows.
thanx.
0
 
LVL 92

Accepted Solution

by:
objects earned 50 total points
ID: 6463302
There is no way to find the number of rows in a ResultSet before processing it.
One workaround is to do get the number of rows using a database query. ie. select count(*) from .....
0
 
LVL 7

Expert Comment

by:RBertora
ID: 6464300
One workaround is to do get the number of rows using a database query. ie. select count(*) from .....


.... and pray nothing hits the database to change the count before your result set is returned to you!

aaayeeee!
0
 
LVL 1

Expert Comment

by:kanthonym
ID: 6464614
ResultSet Rs
ResultSetMetaData meta
int rowCount;
if( rs.next() )
{
  rowCount = meta.getColumnCount();
}


Meta Data does not include row count however, you may be interested in more data about the result set....


Method Summary
 String getCatalogName(int column)
          Gets the designated column's table's catalog name.
 String getColumnClassName(int column)
          Returns the fully-qualified name of the Java class whose instances are manufactured if the method ResultSet.getObject is called to retrieve a value from the column.
 int getColumnCount()
          Returns the number of columns in this ResultSet object.
 int getColumnDisplaySize(int column)
          Indicates the designated column's normal maximum width in characters.
 String getColumnLabel(int column)
          Gets the designated column's suggested title for use in printouts and displays.
 String getColumnName(int column)
          Get the designated column's name.
 int getColumnType(int column)
          Retrieves the designated column's SQL type.
 String getColumnTypeName(int column)
          Retrieves the designated column's database-specific type name.
 int getPrecision(int column)
          Get the designated column's number of decimal digits.
 int getScale(int column)
          Gets the designated column's number of digits to right of the decimal point.
 String getSchemaName(int column)
          Get the designated column's table's schema.
 String getTableName(int column)
          Gets the designated column's table name.
 boolean isAutoIncrement(int column)
          Indicates whether the designated column is automatically numbered, thus read-only.
 boolean isCaseSensitive(int column)
          Indicates whether a column's case matters.
 boolean isCurrency(int column)
          Indicates whether the designated column is a cash value.
 boolean isDefinitelyWritable(int column)
          Indicates whether a write on the designated column will definitely succeed.
 int isNullable(int column)
          Indicates the nullability of values in the designated column.
 boolean isReadOnly(int column)
          Indicates whether the designated column is definitely not writable.
 boolean isSearchable(int column)
          Indicates whether the designated column can be used in a where clause.
 boolean isSigned(int column)
          Indicates whether values in the designated column are signed numbers.
 boolean isWritable(int column)
          Indicates whether it is possible for a write on the designated column to succeed.
0
 
LVL 1

Author Comment

by:steeks
ID: 6466408
Would packaging the count query and data query in a transaction ensure the number returned in the count query is the same as the number of rows in the data query?

Would this work, or are transactions only used for insertions/updates/deletes
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:dnoelpp
ID: 6516590
I think so. Transaction should be for exact this type of a problem.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6956284
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> YOU CANNOT DELETE A QUESTION with comments; special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click this link for Help Desk, Guidelines/Member Agreement and the Question/Answer process.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and please keep them updated. If you are a KnowledgePro user, use the Power Search option to find them.  

Questions which are LOCKED with a Proposed Answer but do not help you, should be rejected with comments added.  When you grade the question less than an A, please comment as to why.  This helps all involved, as well as others who may access this item in the future.  PLEASE DO NOT AWARD POINTS TO ME.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.11686878.html
http://www.experts-exchange.com/questions/Q.20174450.html
http://www.experts-exchange.com/questions/Q.20174444.html
http://www.experts-exchange.com/questions/Q.20179760.html
http://www.experts-exchange.com/questions/Q.20188778.html
http://www.experts-exchange.com/questions/Q.20195508.html
http://www.experts-exchange.com/questions/Q.20239488.html
http://www.experts-exchange.com/questions/Q.20250391.html
http://www.experts-exchange.com/questions/Q.20290851.html




*****  E X P E R T S    P L E A S E  ******  Leave your closing recommendations if this item remains inactive another seven (7) days.  If you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643 
POINTS FOR EXPERTS awaiting comments are listed here -> http://www.experts-exchange.com/commspt/Q.20277028.html
 
Moderators will finalize this question if in @7 days Asker has not responded.  This will be moved to the PAQ (Previously Asked Questions) at zero points, deleted or awarded.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7114473
  Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY );

   ResultSet rst = stmt.executeQuery( strSQL );

   rst.last();
   int numberOfRows = rst.getRow();
   rst.beforeFirst();
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 7118723
Please update and finalize this.
Moondancer - EE Moderator
0
 
LVL 20

Expert Comment

by:Venabili
ID: 8908467
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for objects

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 79
servlet doXXX methods 3 36
fibonacci ten numbers 4 28
servlet filter example 37 42
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now