import java.sql.*; public class Report4J { public static void main (String args[]) { Connection con = null; int iCups, iTotalCups, iEntry; Statement stmt = null; String sDriver = "COM.cloudscape.core.RmiJdbcDriver"; String sURL = "jdbc:cloudscape:rmi:jGuru"; // "jdbc:rmi:jdbc:cloudscape:jGuru;create=true"; String sUsername = "sa"; String sPassword = "admin"; String sCustomer = null, sDOW = null, sType = null; try // Attempt to load the JDBC driver { // with newInstance Class.forName( sDriver ).newInstance(); } catch( Exception e ) // error { System.err.println( "Failed to load current driver."); return; } // end catch try { con = DriverManager.getConnection ( sURL, sUsername, sPassword); stmt = con.createStatement(); } catch ( Exception e) { System.err.println( "problems connecting to " + sURL + ":" ); System.err.println( e.getMessage() ); if( con != null) { try { con.close(); } catch( Exception e2 ) {} } return; } // end catch try { ResultSet result = stmt.executeQuery( "SELECT Entry, Customer, DOW, Cups, Type " + "FROM JJJJData " + "ORDER BY Cups DESC"); if( result.next() ) // get first row { // if data was returned sCustomer = result.getString("Customer"); iCups = result.getInt("Cups"); System.out.println( "On " + result.getString("DOW") + " 4J Customer " + sCustomer + " consumed the most coffee." + " Cups: " + iCups + ", Type: " + result.getString("Type") + ".\n"); iTotalCups = iCups; // increment total while(result.next()) // for each row of data { iEntry = result.getInt("Entry"); sCustomer = result.getString("Customer"); sDOW = result.getString("DOW"); iCups = result.getInt("Cups"); iTotalCups += iCups; // increment total sType = result.getString("Type"); // Report each Customer System.out.println( iEntry + ",\t" + sCustomer + ",\t" + sDOW + ",\t" + iCups + ",\t" + sType ); } // Report total System.out.println( "\n4J Cafe Total Weekly Sales: " + iTotalCups + " cups of coffee."); } // end if( result.next() ) } // end try catch (Exception e) { e.printStackTrace(); } finally { try { stmt.close(); } catch( Exception e ) {} try { con.close(); } catch( Exception e ) {} } // end finally clause } // end main } // end class Report4J