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

jGuru: Help: Handling SQLExceptions and SQLWarnings

 


[Exercise | API Docs | Short Course| Exercises]

Help is available for each task.



    Task 1

    In doUpdate(), add code to get any Connection warnings, using the sqlw variable for the statement;

          stmt = con.createStatement();
    

    If warnings were retrieved, invoke handleSQLWarnings() method; Clear the warnings.

          sqlw = con.getWarnings();
          if( sqlw != null )
          {
            handleSQLWarnings( sqlw,
                               "Conection Warnings: ",
                                null );
            con.clearWarnings();
          }

    Task 2

    In doUpdate(), in the first catch( SQLException SQLe) block, add code to invoke handleSQLExceptions().

          handleSQLExceptions( 
             SQLe,
            "problems connecting to " + sURL + ":",
             null            );
    

    Task 3

    In doUpdate(), after the iProcessed = stmt.executeUpdate( sUpdate ) statement, add code to report if no rows were processed by an SQL statement - use iProcessed, otherwise increment a rows processed total - iProcessedCount.

            if( iProcessed == 0 )
            {
              System.err.println(
                "EURun2 could not process entry:\n" + 
                 sUpdate + "." );
              System.err.println( "Ignore the above " +
                "error line for DDL statements.\n" );        }
            else
            {
              iProcessedCount += iProcessed;
            }

    Task 4

    In doUpdate(), immediately after the previous code, add code to get and handle any Statement warnings, using the sqlw variable.

            sqlw = stmt.getWarnings();
            if( sqlw != null )
            {
              handleSQLWarnings( sqlw,
                                "Statement Warnings: ",
                                 sUpdate );
            }
    

    Task 5

    In the following catch( SQLException SQLe) block, add code to invoke handleSQLExceptions().

          handleSQLExceptions( 
             SQLe,
            "problems with executeUpdate:",
             sUpdate         );

    Task 6

    Add code to report total rows processed in the finally block.

          System.out.println( iProcessedCount + 
                            " rows processed." );
    

    Task 7

    Add code in handleSQLExceptions() to invoke reportSQLExceptions() for each exception retrieved, using while( SQLe != null ) and SQLe = SQLe.getNextException(). After the first invocation, only SQLe and nulls should be passed to reportSQLExceptions().

        while( SQLe != null)
        {
          reportSQLExceptions( SQLe, s, sSQL );
    
          if( bFirstPass )
          {
            s = sSQL = null;
            bFirstPass = false;
          }
    
          SQLe = SQLe.getNextException();
        }
    

    Task 8

    Add code in handleSQLWarnings() to invoke reportSQLExceptions() for each warning retrieved, using while( SQLw != null ) and SQLw = SQLw.getNextWarning(). After the first invocation, only SQLw and nulls should be passed to reportSQLExceptions().

        while( SQLw != null)
        {
          reportSQLExceptions( SQLw, s, sSQL );
    
          if( bFirstPass )
          {
            s = sSQL = null;
            bFirstPass = false;
          }
    
          SQLw = SQLw.getNextWarning();
        }
    

    Task 9

    Insert code in reportSQLExceptions() for complete reporting. If sSQL was not null, get and report the native SQL; If s was not null, report the program-sent text; Get the SQLState and report all available SQLException information; If the SQLState is "01004", report all DataTruncation information.

        if( sSQL != null )
        { // Report the problem SQL
          try
          {
            System.err.println( 
               con.nativeSQL( sUpdate ) );
          }
          catch( Exception e) { /* No action */ }
        }
    
        if( s != null )
        { // Report any program text
          System.err.println( s );
        }
    
        sSQLState = SQLe.getSQLState();
    
        // Report error information
        System.err.println( SQLe.getMessage() );
        System.err.println( "SQL State: " + 
                             sSQLState );
        System.err.println( "Vendor Error Code: " +
                             SQLe.getErrorCode() );
        // check for Data Truncation
        if( sSQLState.equals( "01004") )
        {
          DataTruncation dt = (DataTruncation)(SQLe);
    
          System.err.println( "Data Size: " + 
                               dt.getDataSize() );
          System.err.println( "Transfer Size: " + 
                             dt.getTransferSize() );
          System.err.println( "The index of the " + 
                               ( dt.getParameter() 
                                  ? "parameter " 
                                  : "column " ) +
                              "was " +
                               dt.getIndex() + "." );
    
        }  // end if Data Truncation
    
    

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