import java.sql.*; import java.util.*; public class EBRun { boolean bError = false; Connection con; DatabaseMetaData dbmd; int [] aiupdateCounts; ResourceBundle rb; SQLWarning sqlw; Statement stmt; String sDriver, sDriverKey = "CSDriver", sKey, sPassword, sPasswordKey ="CSPassword", sUpdate, srbName = "ConnectU", srbUpdate, sURL, sURLKey="CSURL", sUserID, sUserIDKey = "CSUserID"; public EBRun() throws MissingResourceException, ClassNotFoundException, InstantiationException, IllegalAccessException { // get the PropertyResourceBundle rb = ResourceBundle.getBundle( srbName ); sDriver = rb.getString( sDriverKey ); sPassword = rb.getString( sPasswordKey ); sURL = rb.getString( sURLKey ); sUserID = rb.getString( sUserIDKey ); // Attempt to load the JDBC driver // with newInstance Class.forName( sDriver ).newInstance(); } // end constructor public void doUpdate( String sargRBName ) throws MissingResourceException, SQLException { int iCount = 0, iProcessed = 0, iProcessedCount = 0; try // get Connection and Statement { con = DriverManager.getConnection ( sURL, sUserID, sPassword); // insert code to determine if // Batch Updates are supported // end insert code con.setAutoCommit( false ); stmt = con.createStatement(); sqlw = con.getWarnings(); if( sqlw != null ) { handleSQLWarnings( sqlw, "Conection Warnings: ", null ); con.clearWarnings(); } } // end try catch ( SQLException SQLe) { handleSQLExceptions( SQLe, "problems connecting to " + sURL + ":", null ); if( con != null) { try { con.commit(); con.close(); } catch( Exception e ) {} } // rethrow the exception throw SQLe; } // end catch try { Enumeration e = null; // get the PropertyResourceBundle // for SQL update statements rb = ResourceBundle.getBundle( sargRBName ); e = rb.getKeys(); // Count keys - keys start at 1. for( ; e.hasMoreElements(); iCount++ ) { (e.nextElement()); }; // use <= since keys start with 1 for( int i = 1; i <= iCount; i++ ) { sKey = "" + i; sUpdate = rb.getString( sKey ); // insert code to use addBatch // end insert code } // end for // insert code to executeBatch() the commands // end insert code sqlw = stmt.getWarnings(); if( sqlw != null ) { System.err.println("from SQLw" ); handleSQLWarnings( sqlw, "Statement Warnings: ", sUpdate ); } } // end try catch( MissingResourceException mre ) { System.err.println( "ResourceBundle problem for " + sargRBName + ", program ends." ); System.err.println("Specific error: " + mre.getMessage() ); throw mre; } // insert a catch block and code // to handle BatchUpdateException // end insert code catch ( SQLException SQLe) { bError = true; handleSQLExceptions( SQLe, "processing problems...", null ); // rethrow the exception throw SQLe; } catch ( RuntimeException e ) { bError = true; throw e; // rethrow the exception } finally { // insert code to report statements submitted, // and results encountered // end insert code try { stmt.close(); } catch( Exception e ) {} try { if( bError ) { con.rollback(); System.err.println( "\nALL Statements for " + sargRBName + " were rolled back due " + "to error condition." ); } else { con.commit(); } con.close(); } catch( Exception e ) {} } // end finally clause } // end doUpdate public void handleSQLExceptions( SQLException SQLe, String s, String sSQL ) { boolean bFirstPass = true; while( SQLe != null) { reportSQLExceptions( SQLe, s, sSQL ); if( bFirstPass ) { s = sSQL = null; bFirstPass = false; } SQLe = SQLe.getNextException(); } } // end handleSQLExceptions public void handleSQLWarnings( SQLWarning SQLw, String s, String sSQL ) { boolean bFirstPass = true; if( s == null ) { s = "SQLWarning:"; } while( SQLw != null) { reportSQLExceptions( SQLw, s, sSQL ); if( bFirstPass ) { s = sSQL = null; bFirstPass = false; } SQLw = SQLw.getNextWarning(); } } // end handleSQLWarnings public void reportSQLExceptions( SQLException SQLe, String s, String sSQL ) { String sSQLState = null; 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 } // end reportSQLExceptions public static void main (String args[]) { boolean bContinue = true; EBRun eurApp = null; if( args.length != 1 ) { System.err.println("Usage: " + "java EBRun SQLUpdateResourceBundleName" ); return; } try { eurApp = new EBRun(); } catch( Exception e ) { System.err.println("Constructor Exception: " + e.getMessage() ); bContinue = false; } if( bContinue ) { try { eurApp.doUpdate( args[0] ); } catch( Exception e ) {} } } // end main } // end class EBRun