Previous Topic

Next Topic

Manage

The manage step provides data management functionality for your application and/or process.

Below is the code for Manage():

   //
   // Manage()
   //
   // Populates table and perform a simple query
   //
   private static void Manage ()
   {
      System.out.println("MANAGE");

      // populate the tables with data
      Add_CustomerMaster_Records();
      Add_CustomerOrders_Records();
      Add_OrderItems_Records();
      Add_ItemMaster_Records();

      // perform a query:
      // list customer name and total amount per order

      // name               total
      // @@@@@@@@@@@@@      $xx.xx

      // for each order in the CustomerOrders table
      //    fetch order number
      //    fetch customer number
      //    fetch name from CustomerMaster table based on customer number
      //    for each order item in OrderItems table
      //       fetch item quantity
      //       fetch item number
      //       fetch item price from ItemMaster table based on item number
      //    next
      // next

      System.out.println("\n\tQuery Results");

      try
      {

         ResultSet rs = stmt.executeQuery (
            "SELECT cm_custname, SUM(im_itempric * oi_quantity) " +
            "FROM custmast, custordr, ordritem, itemmast " +
            "WHERE co_custnumb = cm_custnumb AND co_ordrnumb = oi_ordrnumb AND oi_itemnumb = im_itemnumb " +
            "GROUP BY cm_custnumb, cm_custname");

         // read resultset
         while (rs.next())
         {
            // fetch customer name
            String custname = rs.getString(1);

            // fetch item price
            float total = rs.getFloat(2);

            System.out.println("\t\t" + custname + "\t" + total);
         }

         rs.close();

      }
      catch (SQLException e)
      {
         Handle_Exception(e);
      }
   }


   //
   // Add_CustomerMaster_Records()
   //
   // This function adds records to table CustomerMaster from an
   // array of strings
   //
   private static void Add_CustomerMaster_Records ()
   {
      System.out.println("\tAdd records in table CustomerMaster...");

      String data[] = {
         "('1000','92867','CA','1','Bryan Williams','2999 Regency','Orange')",
         "('1001','61434','CT','1','Michael Jordan','13 Main','Harford')",
         "('1002','73677','GA','1','Joshua Brown','4356 Cambridge','Atlanta')",
         "('1003','10034','MO','1','Keyon Dooling','19771 Park Avenue','Columbia')"
      };

      Delete_Records("custmast");

      try
      {
         // add one record at time to table
         for (int i = 0; i < data.length; i++) {
            stmt.executeUpdate("INSERT INTO custmast VALUES " + data[i]);
         }
      }
      catch (SQLException e)
      {
         Handle_Exception(e);
      }
   }


   //
   // Add_CustomerOrders_Records()
   //
   // This function adds records to table CustomerOrders from an
   // array of strings
   //
   private static void Add_CustomerOrders_Records ()
   {
      System.out.println("\tAdd records in table CustomerOrders...");

      String data[] = {
         "('09/01/2002','09/05/2002','1','1001')",
         "('09/02/2002','09/06/2002','2','1002')"
      };

      Delete_Records("custordr");

      try
      {
         // add one record at time to table
         for (int i = 0; i < data.length; i++) {
            stmt.executeUpdate("INSERT INTO custordr VALUES " + data[i]);
         }
      }
      catch (SQLException e)
      {
         Handle_Exception(e);
      }
   }


   //
   // Add_OrderItems_Records()
   //
   // This function adds records to table OrderItems from an
   // array of strings
   //
   private static void Add_OrderItems_Records ()
   {
      System.out.println("\tAdd records in table OrderItems...");

      String data[] = {
         "(1,2,'1','1')",
         "(2,1,'1','2')",
         "(3,1,'1','3')",
         "(1,3,'2','3')"
      };

      Delete_Records("ordritem");

      try
      {
         // add one record at time to table
         for (int i = 0; i < data.length; i++) {
            stmt.executeUpdate("INSERT INTO ordritem VALUES " + data[i]);
         }
      }
      catch (SQLException e)
      {
         Handle_Exception(e);
      }
   }


   //
   // Add_ItemMaster_Records()
   //
   // This function adds records to table ItemMaster from an
   // array of strings
   //
   private static void Add_ItemMaster_Records ()
   {
      System.out.println("\tAdd records in table ItemMaster...");

      String data[] = {
         "(10,19.95,'1','Hammer')",
         "(3,  9.99,'2','Wrench')",
         "(4, 16.59,'3','Saw')",
         "(1,  3.98,'4','Pliers')"
      };

      Delete_Records("itemmast");

      try
      {
         // add one record at time to table
         for (int i = 0; i < data.length; i++) {
            stmt.executeUpdate("INSERT INTO itemmast VALUES " + data[i]);
         }
      }
      catch (SQLException e)
      {
         Handle_Exception(e);
      }
   }


   //
   // Delete_Records()
   //
   // This function deletes all the records in a tables
   //
   private static void Delete_Records (String table)
   {
      System.out.println("\tDelete records...");

      try
      {
         stmt.executeUpdate("DELETE FROM " + table);
      }
      catch (SQLException e)
      {
         Handle_Exception(e);
      }
   }