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

251 lines
9.4 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<?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 by key" />
<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="rlkeyedfileexample" />
<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 by key</title>
</head>
<body id="rlkeyedfileexample"><a name="rlkeyedfileexample"><!-- --></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 by key</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 by key from a file on the server.
// The user will be prompted for the server name to which to run and
// the library in which to create file QCUSTCDTKY.
//
// Command syntax:
// java RLKeyedFileExample
//
// This program will copy the records from the iSeries Access for Windows sample
// database file (QCUSTCDT in library QIWS) to file QCUSTCDTKY which has
// the same format as QIWS/QCUSTCDT but has set the CUSNUM field as the key
// for the file.
//
// 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 RLKeyedFileExample
{
public static void main(String[] parameters)
{
// Created a reader to get input from the user
BufferedReader inputStream = new BufferedReader(new InputStreamReader(System.in),1);
// Declare variables to hold the system name, library, file and member names
String systemName = "";
String library = "";
// Get the system name from the user
System.out.println();
try
{
System.out.print("System name: ");
systemName = inputStream.readLine();
System.out.print("Library in which to create file QCUSTCDTKY: ");
library = inputStream.readLine();
}
catch(Exception e)
{
System.out.println("Error obtaining user input.");
e.printStackTrace();
System.exit(0);
}
// Create AS400 object and connect for the record level access service.
AS400 system = new AS400(systemName);
try
{
system.connectService(AS400.RECORDACCESS);
}
catch(Exception e)
{
System.out.println("Unable to connect for record level access.");
System.out.println("Check the readme file for special instructions regarding record
level access");
e.printStackTrace();
System.exit(0);
}
RecordFormat qcustcdtFormat = null;
try
{
// Create the RecordFormat object for creating the file. The record format for the new
// file will be the same as the record format for file QIWS/QCUSTCDT. However we will
// make the CUSNUM field a key field.
AS400FileRecordDescription recordDescription =
new AS400FileRecordDescription(system, "/QSYS.LIB/QIWS.LIB/QCUSTCDT.FILE");
// There is only one record format for the file, so take the first (and only) element
// of the RecordFormat array returned as the RecordFormat for the file.
System.out.println("Retrieving record format of QIWS/QCUSTCDT...");
qcustcdtFormat = recordDescription.retrieveRecordFormat()[0];
// Indicate that CUSNUM is a key field
qcustcdtFormat.addKeyFieldDescription("CUSNUM");
}
catch(Exception e)
{
System.out.println("Unable to retrieve record format from QIWS/QCUSTCDT");
e.printStackTrace();
System.exit(0);
}
// Create the keyed file object that represents the
// file we will create on the server. We use a QSYSObectPathName object
// to get the name of the file into the correct format.
QSYSObjectPathName fileName = new QSYSObjectPathName(library,
"QCUSTCDTKY",
"*FILE",
"MBR");
KeyedFile file = new KeyedFile(system, fileName.getPath());
try
{
System.out.println("Creating file " + library + "/QCUSTCDTKY...");
// Create the file using the qcustcdtFormat object
file.create(qcustcdtFormat, "Keyed QCUSTCDT file");
// Populate the file with the records contained in QIWS/QCUSTCDT
copyRecords(system, library);
// Open the file for read-only access. Because we will be randomly
// accessing the file, specify a blocking factor of 1. The
// commit lock level parameter will be ignored since commitment
// control has not been started.
file.open(AS400File.READ_ONLY,
1,
AS400File.COMMIT_LOCK_LEVEL_NONE);
// Assume that we want to display the information for customers
// 192837, 392859 and 938472
// The CUSNUM field is a zoned decimal field of length 6 with
// no decimal positions. Therefore, the key field value is
// represented with a BigDecimal.
BigDecimal[] keyValues = {new BigDecimal(192837),
new BigDecimal(392859),
new BigDecimal(938472)};
// Create the key for reading the records. The key for a KeyedFile
// is specified with an Object[]
Object[] key = new Object[1];
Record data = null;
for (int i = 0; i &lt; keyValues.length; i++)
{
// Setup the key for reading
key[0] = keyValues[i];
// Read the record for customer number keyValues[i]
data = file.read(key);
if (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 the 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"));
}
}
}
// All done with the file
file.close();
// Get rid of the file from the user's system
file.delete();
}
catch(Exception e)
{
System.out.println("Unable to create/read from QTEMP/QCUSTCDT");
e.printStackTrace();
try
{
file.close();
// Get rid of the file from the user's system
file.delete();
}
catch(Exception x)
{
}
}
// All done with record level access; disconnect from the
// record-level access server.
system.disconnectService(AS400.RECORDACCESS);
System.exit(0);
}
public static void copyRecords(AS400 system, String library)
{
// Use the CommandCall class to run the CPYF command to copy the records
// in QIWS/QCUSTCDT to QTEMP/QCUSTCDT
CommandCall c = new CommandCall(system, "CPYF FROMFILE(QIWS/QCUSTCDT) TOFILE("
+ library + "/QCUSTCDTKY) MBROPT(*REPLACE)");
try
{
System.out.println("Copying records from QIWS/QCUSTCDT to "
+ library + "/QCUSTCDTKY...");
c.run();
AS400Message[] msgs = c.getMessageList();
if (!msgs[0].getID().equals("CPC2955"))
{
System.out.println("Unable to populate " + library + "/QCUSTCDTKY");
for (int i = 0; i &lt; msgs.length; i++)
{
System.out.println(msgs[i]);
}
System.exit(0);
}
}
catch(Exception e)
{
System.out.println("Unable to populate " + library + "/QCUSTCDTKY");
System.exit(0);
}
}
}</pre>
</div>
</div>
</div>
</body>
</html>