ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzahh_5.4.0.1/rlreadfileexample.htm

244 lines
9.5 KiB
HTML

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="security" content="public" />
<meta name="Robots" content="index,follow" />
<meta http-equiv="PICS-Label" content='(PICS-1.1 "http://www.icra.org/ratingsv02.html" l gen true r (cz 1 lz 1 nz 1 oz 1 vz 1) "http://www.rsac.org/ratingsv01.html" l gen true r (n 0 s 0 v 0 l 0) "http://www.classify.org/safesurf/" l gen true r (SS~~000 1))' />
<meta name="DC.Type" content="reference" />
<meta name="DC.Title" content="Example: Using record-level access classes to read records from a file" />
<meta name="abstract" content="" />
<meta name="description" content="" />
<meta name="copyright" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Rights.Owner" content="(C) Copyright IBM Corporation 2006" />
<meta name="DC.Format" content="XHTML" />
<meta name="DC.Identifier" content="rlreadfileexample" />
<meta name="DC.Language" content="en-us" />
<!-- All rights reserved. Licensed Materials Property of IBM -->
<!-- US Government Users Restricted Rights -->
<!-- Use, duplication or disclosure restricted by -->
<!-- GSA ADP Schedule Contract with IBM Corp. -->
<link rel="stylesheet" type="text/css" href="./ibmdita.css" />
<link rel="stylesheet" type="text/css" href="./ic.css" />
<title>Example: Using record-level access classes to read records from a file</title>
</head>
<body id="rlreadfileexample"><a name="rlreadfileexample"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Example: Using record-level access classes to read records from a file</h1>
<div><p></p>
<div class="section"><div class="note"><span class="notetitle">Note:</span> Read the <a href="codedisclaimer.htm#codedisclaimer">Code
example disclaimer</a> for important legal information.</div>
</div>
<div class="section"><div class="p"><pre>///////////////////////////////////////////////////////////////////////////////
//
// Record-Level Access example. This program uses the record-level
// access classes to read records from a file on the server.
//
// Command syntax:
// java 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, make a copy of QCUSTCDT and update the copy.
//
// This source is an example of IBM Toolbox for Java "Record-level access".
//
///////////////////////////////////////////////////////////////////////////////
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)
{
String system = "";
// Continue only if a system name was specified.
if (parameters.length &gt;= 1)
{
try
{
// Assume the first parameter is the system name.
system = parameters[0];
// Create an AS400 object for the server that has the file.
AS400 as400 = new AS400(system);
// Create a record description for the file. The file is QCUSTCDT
// in library QIWS.
ZonedDecimalFieldDescription customerNumber =
new ZonedDecimalFieldDescription(new AS400ZonedDecimal(6,0),
"CUSNUM");
CharacterFieldDescription lastName =
new CharacterFieldDescription(new AS400Text(8, as400), "LSTNAM");
CharacterFieldDescription initials =
new CharacterFieldDescription(new AS400Text(3, as400), "INIT");
CharacterFieldDescription street =
new CharacterFieldDescription(new AS400Text(13, as400), "STREET");
CharacterFieldDescription city =
new CharacterFieldDescription(new AS400Text(6, as400), "CITY");
CharacterFieldDescription state =
new CharacterFieldDescription(new AS400Text(2, as400), "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");
// The record format name must be specified for a DDM file.
// In the case of the QCUSTCDT file, its record format is called CUSREC.
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);
// Create the sequential file object that represents the
// file on the server. We use a QSYSObjectPathName 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. Specify a blocking
// factor of 10 (the file object will get 10 records when
// it accesses the server 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();
// Loop 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 server format to
// Java format.
if (((BigDecimal)data.getField("BALDUE")).floatValue() &gt; 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 server.
as400.disconnectAllServices();
}
catch (Exception e)
{
// If any of the above operations failed, print an error message
// 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);
}
}</pre>
</div>
</div>
</div>
</body>
</html>