Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6194341

Example in CachedRowSet javadoc fails to run

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 6
    • 5.0
    • core-libs
    • b81
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.5.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
      Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows XP [Version 5.1.2600]

      A DESCRIPTION OF THE PROBLEM :
      It is not clear from the javadoc of CachedRowSet if nextPage() must be called first when iterating over a CachedRowSet with a pageSize different from 0.

      If you read the doc it seems so, but if you read the example included in the doc it says to do the following:

           CachedRowSet crs = CachedRowSetImpl();
           crs.setPageSize(100);
           crs.execute(conHandle);

           while(crs.next() {
           . . . // operate on first chunk of 100 rows in crs, row by row
           }

           while(crs.nextPage()) {
               while(crs.next()) {
                   . . . // operate on the subsequent chunks (of 100 rows each) in crs,
                         // row by row
               }
           }

      Where clearly nextPage() is not called first but *after* iterating over the first page.

      If you do that in code using reference implementation com.sun.rowset.CachedRowSetImpl, this is failing (see attached example). You have to call nextPage() first for reference implementation to work as expceted.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the attached example (i.e. call next() on CachedRowSet before calling nextPage()). You have to change the private static attributes at the top of the file to connect to an existing database/table.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The list of values of the first column of the row set.
      ACTUAL -
      An exception occurs just after nextPage() is called when calling next()

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.sql.SQLException: Invalid cursor position
      at com.sun.rowset.CachedRowSetImpl.next(Unknown Source)
      at ilog.views.util.data.RowSetPb.test(RowSetPb.java:53)
      at ilog.views.util.data.RowSetPb.main(RowSetPb.java:30)

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------



      import java.sql.SQLException;

      import javax.sql.rowset.CachedRowSet;

      import com.sun.rowset.CachedRowSetImpl;

      /**
       * @author jolif
       *
       */
      public class RowSetPb
      {
        final static String databaseURL = "jdbc:mysql:///test";
        final static String user = "";
        final static String passwd = "";
        final static String driverName = "com.mysql.jdbc.Driver";
        final static String TABLE_NAME = "activities";
           
        public RowSetPb()
        {
        }

        // CJO 10/04
        public static void main(String[] arg)
          throws Exception
        {
          new RowSetPb().test();
        }
        
        public void test() throws ClassNotFoundException, InstantiationException,
                                  IllegalAccessException, SQLException, InterruptedException
        {
          Class.forName(driverName);

          CachedRowSet crs = new CachedRowSetImpl();
          crs.setPageSize(2);
          crs.setUrl(databaseURL);
          crs.setUsername(user);
          crs.setPassword(passwd);
          crs.setCommand("select * from "+TABLE_NAME);
          crs.execute();
          

          while(crs.next()) {
            // operate on first chunk of 2 rows in crs, row by row
            System.out.println(crs.getObject(1));
          }

            while(crs.nextPage()) {
                while(crs.next()) {
                    // operate on the subsequent chunks (of 2 rows each) in crs,
                    // row by row
                    System.out.println(crs.getObject(1));
                }
            }
        }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Call nextPage() just after executing the RowSet. However this is contradictory with what is specified in the javadoc sample.
      ###@###.### 2004-11-11 23:20:49 GMT

            ahandasunw Amit Handa (Inactive)
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: