Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 888
  • Last Modified:

getting ResultSet row count

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
steeks
Asked:
steeks
1 Solution
 
pagladasuCommented:
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
 
objectsCommented:
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
 
RBertoraCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
kanthonymCommented:
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
 
steeksAuthor Commented:
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
 
dnoelppCommented:
I think so. Transaction should be for exact this type of a problem.
0
 
MoondancerCommented:
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
 
knightEknightCommented:
  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
 
MoondancerCommented:
Please update and finalize this.
Moondancer - EE Moderator
0
 
VenabiliCommented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now