Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

jGuru: Help: Using Batch Updates

 


[Exercise | API Docs | Short Course| Exercises]

Help is available for each task.

    Task 1

    In doUpdate() get DatabaseMetaData using dbmd and determine if the driver supports Batch Updates. Report, close the Connection and return if Batch Updates are not supported.

          dbmd = con.getMetaData();
          if( !dbmd.supportsBatchUpdates() )
          {
            System.err.println( "EBRun Error: Driver " + 
              "does not support Batch Updates." );
            System.err.println( "Program ends..." );
            try { con.close(); }
            catch( Exception e ) {}
            return;
          }
    

    Task 2

    In doUpdate(), insert code to add the sUpdate SQL statements to stmt's list of commands.

            stmt.addBatch( sUpdate );
    

    Task 3

    In doUpdate(), after all commands have been added, perform the executeBatch method, returning the update count array to aiupdateCounts.

          aiupdateCounts = stmt.executeBatch();
    

    Task 4

    In doUpdate(), add a catch block to handle BatchUpdateExceptions. Put the result from BatchUpdateException.UpdateCounts() into aiupdateCounts.

        catch( BatchUpdateException bue )
        {
          bError = true;
          aiupdateCounts = bue.getUpdateCounts();
    
          handleSQLExceptions( 
             bue,
            "BatchUpdateException:",
             null            );
          throw bue; // rethrow the exception
        }
    

    Task 5

    In doUpdate(), in the finally block, add code to report statements submitted and results encountered. Report each possible outcome for each element in the aiupdateCounts array.

          System.err.println( "Update Count: " + iCount +
            " statements submitted.");
    
          System.err.println( "For statement number: ");
          for (int i = 0; i < aiupdateCounts.length; i++)
          {
            iProcessed = aiupdateCounts[i];
            if( iProcessed == -3 )
            {
              System.err.println( ( i+1 ) + 
                " was in error." );
              bError = true; // should be true, enforce
              continue;
            }
    
            if( iProcessed == -2 )
            {
              System.err.println( ( i+1 ) + 
                " was successful, but " +
                "affected rowcount is unknown." );
              continue;
            }
    
            if( iProcessed == 0 )
            {
              System.err.println( ( i+1 ) + 
                " EBRun enforced rollback, " + 
                "affected rowcount was zero." );
              bError = true;
              continue;
            }
            if( iProcessed > 0 )
            {
              System.err.println( ( i+1 ) + 
                " was successful, affected rowcount: " +
                 iProcessed );
              iProcessedCount += iProcessed;
              continue;
            }
    
            System.err.println( ( i+1 ) + 
              " was unknown result, affected rowcount: " +
               iProcessed + ", not included in total." );
          } // end for
    
          System.err.println( aiupdateCounts.length +
            " statements processed, " + iProcessedCount +
            " rows affected. ");
    

Copyright 1996-2000 jGuru.com. All Rights Reserved.