[Next Example | Main Tutorial Page]

RLA: Example 1 of 2

Use the following as an example for your program.


//////////////////////////////////////////////////////////////////////////////////
//
// Record level access example.  This program will prompt the user
// for the name of the AS/400 and the file to display.  The file must exist
// and should contain records.  Each record in the file will be displayed
// to System.out.
//
// Calling syntax: java RLSequentialAccessExample
//////////////////////////////////////////////////////////////////////////////////
//
// This source is an example of IBM Toolbox for Java "RecordLevelAccess"
// IBM grants you a nonexclusive license to use this as an example
// from which you can generate similar function tailored to
// your own specific needs.
//
// This sample code is provided by IBM for illustrative purposes
// only. These examples have not been thoroughly tested under all
// conditions. IBM, therefore, cannot guarantee or imply
// reliability, serviceability, or function of these programs.
//
// All programs contained herein are provided to you "AS IS"
// without any warranties of any kind.  The implied warranties of
// merchantability and fitness for a particular purpose are
// expressly disclaimed.
//
// IBM Toolbox for Java
// (C) Copyright IBM Corp. 1997
// All rights reserved.
// US Government Users Restricted Rights -
// Use, duplication, or disclosure restricted
// by GSA ADP Schedule Contract with IBM Corp.
//
//////////////////////////////////////////////////////////////////////////////////



import java.io.*;
import java.util.*;
import com.ibm.as400.access.*;

public class RLSequentialAccessExample
{
   public static void main(String[] parameters)
   {
      BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in),1);

      String systemName = "";
      String library = "";
      String file = "";
      String member = "";

      System.out.println();
      try
      {
        System.out.print("System name: ");
        systemName = inputStream.readLine();

        System.out.print("Library in which the file exists: ");
        library = inputStream.readLine();

        System.out.print("File name: ");
        file = inputStream.readLine();

        System.out.print("Member name (press enter for first member): ");
        member = inputStream.readLine();
        if (member.equals(""))
        {
          member = "*FIRST";
        }

        System.out.println();
      }
      catch (Exception e)
      {
        System.out.println("Error obtaining user input.");
        e.printStackTrace();
        System.exit(0);
      }

      
      AS400 system = new AS400(systemName);1 Click to display a detailed explanation
      try
      {
        system.connectService(AS400.RECORDACCESS);
      }
      catch(Exception e)
      {
        System.out.println("Unable to connect for record level access.");
        System.out.println("Check the programmer's guide setup file for special instructions regarding record level access");
        e.printStackTrace();
        System.exit(0);
      }

      
      QSYSObjectPathName filePathName = new QSYSObjectPathName(library, file, member, "MBR");2 Click to display a detailed explanation

      
      SequentialFile theFile = new SequentialFile(system, filePathName.getPath());3 Click to display a detailed explanation

      
      AS400FileRecordDescription recordDescription = new AS400FileRecordDescription(system, filePathName.getPath());
      try
      {
        RecordFormat[] format = recordDescription.retrieveRecordFormat();4 Click to display a detailed explanation

        
        theFile.setRecordFormat(format[0]);5 Click to display a detailed explanation

        
        theFile.open(AS400File.READ_ONLY, 100, AS400File.COMMIT_LOCK_LEVEL_NONE);6 Click to display a detailed explanation

        System.out.println("Displaying file " + library.toUpperCase() + "/" + file.toUpperCase() + "(" + theFile.getMemberName().trim() + "):");
		
        Record record = theFile.readNext();7 Click to display a detailed explanation
        while (record != null)
        {
          System.out.println(record);
          record = theFile.readNext();
        }
        System.out.println();

        
        theFile.close();8 Click to display a detailed explanation

        
        system.disconnectService(AS400.RECORDACCESS);9 Click to display a detailed explanation
      }
      catch (Exception e)
      {
        System.out.println("Error occurred attempting to display the file.");
        e.printStackTrace();

        try
        {
          // Close the file
          theFile.close();
        }
        catch(Exception x)
        {
        }

        system.disconnectService(AS400.RECORDACCESS);
        System.exit(0);
      }

      // Make sure that the application ends; see readme for details
      System.exit(0);
    }
  }
  

  1. This line of code creates an AS/400 object and connects to the record-level access service.

  2. This line creates a QSYSObjectPathName object that obtains the integrated file system path name form of the object to be displayed.

  3. This statement creates an object that represents an existing sequential file on the AS/400 you are connected to. This sequential file is the file that will be displayed.

  4. These lines retrieve the record format of the file.

  5. This line sets the record format for the file.

  6. This line opens the selected file for reading. It will read 100 records at a time, when it is possible.

  7. This line of code reads each record in sequence.

  8. This line closes the file.

  9. This line disconnects from the record-level access service.




Previous

Next