Record-Level Access Example
///////////////////////////////////////////////////////////////////////////////
//
// Record-Level Access example. This program uses the record-level
// access classes to read records from an AS/400 file.
//
// Command syntax:
// RLReadFile system
//
// This program reads the records from CA/400's sample database file
// (qcustcdt in library QIWS). If you change this example to update
// records you should make a copy of qcustcdt and update the copy.
//
///////////////////////////////////////////////////////////////////////////////
//
// This source is an example of AS/400 Toolbox for Java "Record-level access".
// 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
// merchantablility and fitness for a particular purpose are
// expressly disclaimed.
//
// AS/400 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 java.math.*;
import com.ibm.as400.access.*;
public class RLReadFile extends Object
{
public static void main(String[] parameters)
{
System.out.println( " " );
String system = "";
// Continue only if a system name was specified.
if (parameters.length >= 1)
{
// Assume the first parameter is the system name.
system = parameters[0];
// Create a record description for the file. The file is QCUSTCDT
// in library QIWS.
ZonedDecimalFieldDescription customerNumber =
new ZonedDecimalFieldDescription(new AS400ZonedDecimal(6,0),
"CUSNUM");
// You need to specify the correct CCSID of the character field
// when running to DBCS systems. For example, when the CCSID of
// the character field is 290, you need to have the following
// statement:
//
// CharacterFieldDescription lastName =
// new CharacterFieldDescription(new AS400Text(8, 290), "LSTNAM");
CharacterFieldDescription lastName =
new CharacterFieldDescription(new AS400Text(8), "LSTNAM");
CharacterFieldDescription initials =
new CharacterFieldDescription(new AS400Text(3), "INIT");
CharacterFieldDescription street =
new CharacterFieldDescription(new AS400Text(13), "STREET");
CharacterFieldDescription city =
new CharacterFieldDescription(new AS400Text(6), "CITY");
CharacterFieldDescription state =
new CharacterFieldDescription(new AS400Text(2), "STATE");
ZonedDecimalFieldDescription zipCode =
new ZonedDecimalFieldDescription(new AS400ZonedDecimal(5,0),
"ZIPCOD");
ZonedDecimalFieldDescription creditLimit =
new ZonedDecimalFieldDescription(new AS400ZonedDecimal(4,0),
"CDTLMT");
ZonedDecimalFieldDescription chargeCode =
new ZonedDecimalFieldDescription(new AS400ZonedDecimal(1,0),
"CHGCOD");
ZonedDecimalFieldDescription balanceDue =
new ZonedDecimalFieldDescription(new AS400ZonedDecimal(6,2),
"BALDUE");
ZonedDecimalFieldDescription creditDue =
new ZonedDecimalFieldDescription(new AS400ZonedDecimal(6,2),
"CDTDUE");
RecordFormat qcustcdt = new RecordFormat("CUSREC");
qcustcdt.addFieldDescription(customerNumber);
qcustcdt.addFieldDescription(lastName);
qcustcdt.addFieldDescription(initials);
qcustcdt.addFieldDescription(street);
qcustcdt.addFieldDescription(city);
qcustcdt.addFieldDescription(state);
qcustcdt.addFieldDescription(zipCode);
qcustcdt.addFieldDescription(creditLimit);
qcustcdt.addFieldDescription(chargeCode);
qcustcdt.addFieldDescription(balanceDue);
qcustcdt.addFieldDescription(creditDue);
try
{
// Create an AS400 object for the AS/400 system that has the file.
AS400 as400 = new AS400(system);
// Create the sequential file object that represents the
// file on the AS/400. We use a QSYSObectPathName object
// to get the name of the file into the correct format.
QSYSObjectPathName fileName = new QSYSObjectPathName("QIWS",
"QCUSTCDT",
"FILE");
SequentialFile file = new SequentialFile(as400, fileName.getPath());
// Let the file object know the format of the records.
file.setRecordFormat(qcustcdt);
// Open the file for read-only access. Specifiy a blocking
// factor of 10 (the file object will get 10 records when
// it accesses the AS/400 for data). Do not use commitment
// control.
file.open(SequentialFile.READ_ONLY,
10,
SequentialFile.COMMIT_LOCK_LEVEL_NONE);
// Read the first record of the file.
Record data = file.readNext();
// While there are records in the file (while we have not
// reached end-of-file.
while(data != null)
{
// Display the record only if balance due is greater than
// zero. In that case display the customer name and
// the balance due. The following code pulls fields out
// of the record by field name. As the field is retrieved
// from the record it is converted from AS/400 format to
// Java format.
if (((BigDecimal)data.getField("BALDUE")).floatValue() > 0.0)
{
System.out.print((String) data.getField("INIT") + " ");
System.out.print((String) data.getField("LSTNAM") + " ");
System.out.println((BigDecimal) data.getField("BALDUE"));
}
// Read the next record in the file.
data = file.readNext();
}
// When there are no more records to read disconnect from the
// record-level access server.
as400.disconnectService(AS400.RECORDACCESS);
}
catch (Exception e)
{
// If any of the above operations failed say the read failed
// and output the exception.
System.out.println("Could not read the file");
System.out.println(e);
}
}
// Display help text when parameters are incorrect.
else
{
System.out.println("");
System.out.println("");
System.out.println("");
System.out.println("Parameters are not correct. Command syntax is:");
System.out.println("");
System.out.println(" RLReadFile as400");
System.out.println("");
System.out.println("Where");
System.out.println("");
System.out.println(" as400 = system that contains the file");
System.out.println("");
System.out.println("For example:");
System.out.println("");
System.out.println(" RLReadFile mySystem");
System.out.println("");
System.out.println("");
System.out.println("Note, this program reads data base file QIWS/QCUSTCDT. ");
System.out.println("");
System.out.println("");
}
System.exit(0);
}
}