0

'm trying to get the number of rows from the result set using following code.

  stmt = Connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY
);

System.out.println("Rows returned: " + getResultSetSize(rs));

and my getResultSetSize definition is

public static int getResultSetSize(ResultSet rs) {
int size = 0;
try {           
rs.last();
size = rs.getRow();
rs.beforeFirst();
} catch (Exception ex) {
System.out.println("Exception"+ex.getMessage());
}
return size;
}

It's fetching number of rows for one query and for other queries it's throwing an exception Result set type is TYPE_FORWARD_ONLY at sun.jdbc.odbc.JdbcOdbcResultSet.isLast(Unknown Source)

I can print the resultset but I could not number of rows from it. I did not know where I have been wrong.I've even tried with Prepared Statement.But I got same result. Please suggest me a way to get out of this. Thanks in advance.

Svp57
  • 308
  • 2
  • 13
  • What database do you use? Some database drivers don't provide this feature. – Ivan Ivanov Jul 11 '14 at 06:53
  • Are all your statements built as `TYPE_SCROLL_INSENSITIVE`? Default statements are `TYPE_FORWARD_ONLY`. – Daniel Jul 11 '14 at 06:54
  • @Daniel all my statements are bulit as TYPE_SCROLL_INSENSITIVE only.. – Svp57 Jul 11 '14 at 09:44
  • Javadoc says : "SQLException - if a database access error occurs; this method is called on a closed result set or the result set type is TYPE_FORWARD_ONLY". You probably missed something. Print also the stacktrace for the exception and show us exactly where the problem comes from. Your method looks fine to me, but I would avoid this approach as much as possible because it is error prone and adds unnecessary performance penalty compared to TYPE_FORWARD_ONLY result sets. I suggest you reading [this post](http://stackoverflow.com/questions/192078/how-do-i-get-the-size-of-a-java-sql-resultset) – Daniel Jul 11 '14 at 10:20
  • @Daniel :There is no database access issue because I can traverse through the Resultset and print it even.The only problem I got is to retieve count. – Svp57 Jul 11 '14 at 10:39
  • Of course you can normally traverse a result set(from first to last), but the problem comes from the fact that you positionate your cursor before first element, and in case your result set is not explicitly built as TYPE_SCROLL_INSENSITIVE , you will have an error. That's why I've mentioned this way to be error prone, and I for sure suggest you to choose a different approach. – Daniel Jul 11 '14 at 13:49
  • @Ivon Ivanov my database is Composite DVL(Data Virtualisation Layer) technology. – Svp57 Jul 11 '14 at 16:12

0 Answers0