< Previous | Next >

Completed CRUDTest.egl file after lesson 2

This code is the completed version of the CRUDTest.egl file. If you see any errors marked by red X symbols in the file, make sure your code matches this code:
package programs;

import eglderbyr7.data.*;

program CRUDTest type BasicProgram {}
  
  function main()
    
    // Print the starting records in the database.
    SysLib.writeStdout("Contents of database before any SQL operations:");
    printAllCustomers();
    
    // Add a record.
    addCustomer();
    
    // Update the record.
    updateCustomer();
    
    // Delete a record.
    deleteCustomer();
    
  end
  
  function addCustomer()
    customerToAdd Customer;
    customerToAdd.CustomerId = 50;
    customerToAdd.FirstName = "John";
    customerToAdd.LastName = "Doe";
    
    // Insert the record into the database
    try
      add customerToAdd;
      SysLib.writeStdout("Contents of database after adding a new record:");
      printAllCustomers();
    onException(exception SQLException)
      handleDBException(exception);
    end
    
  end
    
  function deleteCustomer()
    customerToDelete Customer;
    customerToDelete.CustomerId = 50;
    
    // Delete the record.
    try
      delete customerToDelete noCursor;
      SysLib.writeStdout("Contents of database after deleting the record:");
      printAllCustomers();
    onException(exception SQLException)
      handleDBException(exception);
    end
    
  end
  
  function updateCustomer()
    customerToUpdate Customer;
    customerToUpdate.CustomerId = 50;
    
    // Retrieve the row.
    get customerToUpdate;
    
    // Change values in the record variable.
    customerToUpdate.FirstName = "Johnny";
    customerToUpdate.LastName = "Five";
    
    // Update the row in the database.
    try
      replace customerToUpdate noCursor;
      SysLib.writeStdout("Contents of database after updating the record:");
      printAllCustomers();
    onException(exception SQLException)
      handleDBException(exception);
    end
    
  end
  
  // This function prints out the contents of the CUSTOMER
  // table in the database.
  function printAllCustomers()
    
    allCustomers Customer[0];

    get allCustomers;
    for (counter int from 1 to allcustomers.getSize())
      SysLib.writeStdout(allCustomers[counter].CustomerId::" "::
        allCustomers[counter].FirstName::" "::
        allCustomers[counter].LastName);
    end
    SysLib.writeStdout("\n");

  end
  
  //This function responds to errors accessing the database.
  function handleDBException(exception SQLException)
    SysLib.writeStdout("Error accessing database. "::
      "See Troubleshooting section");
    SysLib.writeStdout(exception.message);
  end
  
end

Return to Lesson 2: Basic SQL operations in EGL.

< Previous | Next >

Feedback