On 04/13/10 10:42 AM, Ян Программист wrote:
> I have a stupid question: what is the best to count rows in result
> set? :-)))
>
> Cause that code is not working:
>
> private static int getRowCount(java.sql.ResultSet columnSet) throws 
> SQLException
>       { 
>               int rowCount;  
>                  int currentRow = columnSet.getRow();            // Get 
> current row  
>                  rowCount = columnSet.last() ? columnSet.getRow() : 0; // 
> Determine number of rows  
>                  if (currentRow == 0)                      // If there was no 
> current row  
>                     columnSet.beforeFirst();                     // We want 
> next() to go to first row  
>                  else                                      // If there WAS a 
> current row  
>                     columnSet.absolute(currentRow);              // Restore 
> it  
>                  return rowCount;     
>       }
>   
> java.sql.SQLException: Метод 'getRow()' допускается только в курсорах с 
> прокруткой.
>       at 
> org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown 
> Source)
>       at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
>       at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
>       at org.apache.derby.impl.jdbc.EmbedResultSet.checkScrollCursor(Unknown 
> Source)
>       at org.apache.derby.impl.jdbc.EmbedResultSet.getRow(Unknown Source)
>       at DatabaseMetaDataTest.getRowCount(DatabaseMetaDataTest.java:137)
>       at DatabaseMetaDataTest.testGetColumns(DatabaseMetaDataTest.java:132)
>       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>       at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>       at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>       at java.lang.reflect.Method.invoke(Method.java:597)
>       at 
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
>       at 
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
>       at 
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
>       at 
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
>       at 
> org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
>       at 
> org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
>       at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
>       at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
>       at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
>       at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
>       at 
> org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
>       at 
> org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
>       at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
>       at 
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
>       at 
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>       at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
>       at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
>       at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
>       at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
> Caused by: java.sql.SQLException: Метод 'getRow()' допускается только в 
> курсорах с прокруткой.
>       at 
> org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
>       at 
> org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown
>  Source)
>       ... 31 more
>
>
> Translating for those who does not know Russian: "Method 'getRow' is
> acceptable only in scrollable cursors"
>
> I made JUnit test case for metadata stuff; can't proceed because of
> that bug. John

The limitation that ResultSet.getRow() does not work on forward-only
result sets is logged here: https://issues.apache.org/jira/browse/DERBY-4251

But even if that issue is fixed, your getRowCount() method won't work on
forward-only result sets because it uses absolute(), beforeFirst() and
last(). The JDBC specification states that those methods should throw an
SQLException if the result set type is TYPE_FORWARD_ONLY.

So I think currently the only way to get the row count of a forward-only
result set is to count the number of times ResultSet.next() returns true.

-- 
Knut Anders

  • JDBC result set Ян Программист
    • Re: JDBC result set Knut Anders Hatlen

Reply via email to