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

196 lines
10 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="KeyedFile" />
<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="rlakey" />
<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>KeyedFile</title>
</head>
<body id="rlakey"><a name="rlakey"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">KeyedFile</h1>
<div><p></p>
<div class="section"><p>The <a href="javadoc/com/ibm/as400/access/KeyedFile.html#NAVBAR_TOP">KeyedFile</a> class gives a Java™ program keyed access to a file on the
server. Keyed access means that the Java program can access the records of a
file by specifying a key. Methods exist to position the cursor, read, update,
and delete records by key.</p>
</div>
<div class="section"><p>To position the cursor, use the following methods:</p>
</div>
<div class="section"><ul><li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#POSITIONCURSOR(BYTE[], INT)">positionCursor(Object[])</a> - set cursor to the first record
with the specified key.</li>
<li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#POSITIONCURSORAFTER(BYTE[], INT)">positionCursorAfter(Object[])</a> - set cursor to the record
after the first record with the specified key.</li>
<li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#POSITIONCURSORBEFORE(BYTE[], INT)">positionCursorBefore(Object[])</a> - set cursor to the record
before the first record with the specified key.</li>
</ul>
</div>
<div class="section"><p>To delete a record, use the following method :</p>
</div>
<div class="section"><ul><li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#DELETERECORD(BYTE[], INT)">deleteRecord(Object[])</a> - delete the first record with
the specified key.</li>
</ul>
</div>
<div class="section"><p>The read methods are:</p>
</div>
<div class="section"><ul><li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#READ(JAVA.LANG.OBJECT[])">read(Object[])</a> - read the first record with the specified
key.</li>
<li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#READAFTER(BYTE[], INT)">readAfter(Object[])</a> - read the record after the first
record with the specified key.</li>
<li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#READBEFORE(BYTE[], INT)">readBefore(Object[])</a> - read the record before the first
record with the specified key.</li>
<li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#READNEXTEQUAL(BYTE[], INT)">readNextEqual()</a> - read the next record whose key matches
the specified key. Searching starts from the record after the current cursor
position.</li>
<li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#READPREVIOUSEQUAL(BYTE[], INT)">readPreviousEqual()</a> - read the previous record whose
key matches the specified key. Searching starts from the record before the
current cursor position.</li>
</ul>
</div>
<div class="section"><p>To update a record, use the following method:</p>
</div>
<div class="section"><ul><li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#UPDATE(BYTE[], COM.IBM.AS400.ACCESS.RECORD, INT)">update(Object[])</a> - update the record with the specified
key.</li>
</ul>
</div>
<div class="section"><p>Methods are also provided for specifying a search criteria when
positioning, reading, and updating by key. Valid search criteria values are
as follows:</p>
</div>
<div class="section"><ul><li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#KEY_EQ">Equal</a> - find the first record whose key matches the
specified key.</li>
<li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#KEY_LT">Less than</a> - find the last record whose key comes before
the specified key in the key order of the file.</li>
<li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#KEY_LE">Less than or equal</a> - find the first record whose key
matches the specified key. If no record matches the specified key, find the
last record whose key comes before the specified key in the key order of the
file.</li>
<li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#KEY_GT">Greater than</a> - find the first record whose key comes
after the specified key in the key order of the file.</li>
<li><a href="javadoc/com/ibm/as400/access/KeyedFile.html#KEY_GE">Greater than or equal</a> - find the first record whose key
matches the specified key. If no record matches the specified key, find the
first record whose key comes after the specified key in the key order of the
file.</li>
</ul>
</div>
<div class="section"><p>KeyedFile is a subclass of AS400File; all methods in AS400File
are available to KeyedFile. </p>
</div>
<div class="section"><p><strong><span class="synph" id="rlakey__keyspec"><a name="rlakey__keyspec"><!-- --></a><span class="kwd"></span></span>Specifying the key</strong></p>
</div>
<div class="section"><p>The key for a KeyedFile object is represented by an array of Java Objects
whose types and order correspond to the types and order of the key fields
as specified by the <a href="javadoc/com/ibm/as400/access/RecordFormat.html">RecordFormat</a> object for the file.</p>
</div>
<div class="section"><p>The following example shows how to specify the key for the KeyedFile
object.</p>
</div>
<div class="section"><div class="p"><pre> // Specify the key for a file whose key fields, in order,
// are:
// CUSTNAME CHAR(10)
// CUSTNUM BINARY(9)
// CUSTADDR CHAR(100)VARLEN()
// Note that the last field is a variable-length field.
Object[] theKey = new Object[3];
theKey[0] = "John Doe";
theKey[1] = new Integer(445123);
theKey[2] = "2227 John Doe Lane, ANYTOWN, NY 11199"; </pre>
</div>
</div>
<div class="section"><p>A KeyedFile object accepts partial keys as well as complete keys.
However, the key field values that are specified must be in order.</p>
</div>
<div class="section"><p>For example:</p>
</div>
<div class="section"><div class="p"><pre> // Specify a partial key for a file whose key fields,
// in order, are:
// CUSTNAME CHAR(10)
// CUSTNUM BINARY(9)
// CUSTADDR CHAR(100)VARLEN()
Object[] partialKey = new Object[2];
partialKey[0] = "John Doe";
partialKey[1] = new Integer(445123);
// Example of an INVALID partial key
Object[] INVALIDPartialKey = new Object[2];
INVALIDPartialKey[0] = new Integer(445123);
INVALIDPartialKey[1] = "2227 John Doe Lane, ANYTOWN, NY 11199";</pre>
</div>
<p>Null keys and null key fields are not supported.</p>
<p>The
key field values for a record can be obtained from the <a href="javadoc/com/ibm/as400/access/Record.html">Record</a> object
for a file through the <a href="javadoc/com/ibm/as400/access/Record.html#GETKEYFIELDS()"> getKeyFields()</a> method.</p>
<p>The following example shows
how to read from a file by key:</p>
<div class="p"><pre> // Create an AS400 object, the file exists on this
// server.
AS400 sys = new AS400("mySystem.myCompany.com");
// Create a file object that represents the file
KeyedFile myFile = new KeyedFile(sys, "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/%FILE%.MBR");
// Assume that the AS400FileRecordDescription class
// was used to generate the code for a subclass of
// RecordFormat that represents the record format
// of file MYFILE in library MYLIB. The code was
// compiled and is available for use by the Java program.
RecordFormat recordFormat = new MYKEYEDFILEFormat();
// Set the record format for myFile. This must
// be done before invoking open()
myFile.setRecordFormat(recordFormat);
// Open the file.
myFile.open(AS400File.READ_WRITE, 0, AS400File.COMMIT_LOCK_LEVEL_NONE);
// The record format for the file contains
// four key fields, CUSTNUM, CUSTNAME, PARTNUM
// and ORDNUM in that order.
// The partialKey will contain 2 key field
// values. Because the key field values must be
// in order, the partialKey will consist of values for
// CUSTNUM and CUSTNAME.
Object[] partialKey = new Object[2];
partialKey[0] = new Integer(1);
partialKey[1] = "John Doe";
// Read the first record matching partialKey
Record keyedRecord = myFile.read(partialKey);
// If the record was not found, null is returned.
if (keyedRecord != null)
{ // Found the record for John Doe, print out the info.
System.out.println("Information for customer " + (String)partialKey[1] + ":");
System.out.println(keyedRecord);
}
....
// Close the file since I am done using it
myFile.close();
// Disconnect since I am done using record-level access
sys.disconnectService(AS400.RECORDACCESS);</pre>
</div>
</div>
</div>
</body>
</html>