import java.sql.*; import java.util.*; public class EURun2 { Connection con; 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 EURun2() 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 iProcessed = 0, iProcessedCount =0; try // get Connection and Statement { con = DriverManager.getConnection ( sURL, sUserID, sPassword); 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.close(); } catch( Exception e ) {} } // rethrow the exception throw SQLe; } // end catch try { Enumeration e = null; int iCount = 0; // 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 ); iProcessed = stmt.executeUpdate( sUpdate ); 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; } sqlw = stmt.getWarnings(); if( sqlw != null ) { handleSQLWarnings( sqlw, "Statement Warnings: ", sUpdate ); } } // end for } // end try catch( MissingResourceException mre ) { System.err.println( "ResourceBundle problem for " + sargRBName + ", program ends." ); System.err.println("Specific error: " + mre.getMessage() ); throw mre; } catch ( SQLException SQLe) { handleSQLExceptions( SQLe, "problems with executeUpdate:", sUpdate ); // rethrow the exception throw SQLe; } finally { System.out.println( iProcessedCount + " rows processed." ); try { stmt.close(); } catch( Exception e ) {} try { 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; EURun2 eurApp = null; if( args.length != 1 ) { System.err.println("Usage: " + "java EURun2 SQLUpdateResourceBundleName" ); return; } try { eurApp = new EURun2(); } 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 EURun2