< 이전 | 다음 >

학습 2에서 완성된 CRUDTest.egl 파일

이 코드는 학습 2에서 완성된 CRUDTest.egl 파일입니다. 파일에 빨간색 X 기호로 표시된 오류가 나타나면 사용자의 코드가 다음 코드와 일치하는지 확인하십시오.

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

학습 2: EGL의 기본 SQL 조작으로 되돌아가십시오.

< 이전 | 다음 >