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

149 lines
7.5 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="Reading and writing records" />
<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="rlaread" />
<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>Reading and writing records</title>
</head>
<body id="rlaread"><a name="rlaread"><!-- --></a>
<!-- Java sync-link --><script language="Javascript" src="../rzahg/synch.js" type="text/javascript"></script>
<h1 class="topictitle1">Reading and writing records</h1>
<div><p></p>
<div class="section"><p>You can use the <a href="javadoc/com/ibm/as400/access/AS400File.html#NAVBAR_TOP"> AS400File</a> class to read, write, update,
and delete records in files on the server. The record is accessed through
the <a href="dtadrec.htm#dtadrec">Record</a> class, which is described
by a <a href="dtadrfmt.htm#dtadrfmt">RecordFormat</a> class. The record
format must be set through the <a href="javadoc/com/ibm/as400/access/AS400File.html#SETRECORDFORMAT(INT)"> setRecordFormat()</a> method before the file
is opened, unless the file was just created (without an intervening close())
by one of the <a href="javadoc/com/ibm/as400/access/AS400File.html#CREATE(JAVA.LANG.STRING, JAVA.LANG.STRING)"> create()</a> methods, which sets the record
format for the object.</p>
</div>
<div class="section"><p>Use the read() methods to read a record from the file. Methods
are provided to do the following:</p>
</div>
<div class="section"><ul><li><a href="javadoc/com/ibm/as400/access/AS400File.html#READ()"> read()</a> - read the record at the current cursor position</li>
<li><a href="javadoc/com/ibm/as400/access/AS400File.html#READFIRST()"> readFirst()</a> - read the first record of the file</li>
<li><a href="javadoc/com/ibm/as400/access/AS400File.html#READLAST()"> readLast()</a> - read the last record of the file</li>
<li><a href="javadoc/com/ibm/as400/access/AS400File.html#READNEXT()"> readNext()</a> - read the next record in the file</li>
<li><a href="javadoc/com/ibm/as400/access/AS400File.html#READPREVIOUS()"> readPrevious()</a> - read the previous record
in the file</li>
</ul>
</div>
<div class="section"><p>The following example shows how to use the readNext() method:</p>
</div>
<div class="section"><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
SequentialFile myFile = new SequentialFile(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 MYFILEFormat();
// Set the record format for myFile. This must
// be done before invoking open()
myFile.setRecordFormat(recordFormat);
// Open the file.
myFile.open(AS400File.READ_ONLY, 0, AS400File.COMMIT_LOCK_LEVEL_NONE);
// Read each record in the file writing field
// CUSTNAME to System.out
System.out.println(" CUSTOMER LIST");
System.out.println("____________________________________________");
Record record = myFile.readNext();
while(record != null)
{
System.out.println(record.getField("CUSTNAME"));
record = myFile.readNext();
}
....
// 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 class="section"><p>Use the <a href="javadoc/com/ibm/as400/access/AS400File.html#UPDATE(COM.IBM.AS400.ACCESS.RECORD)"> update()</a> method to update the record at
the cursor position.</p>
</div>
<div class="section"><p>For example:</p>
</div>
<div class="section"><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
SequentialFile myFile = new SequentialFile(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 MYFILEFormat();
// Set the record format for myFile. This must
// be done prior to invoking open()
myFile.setRecordFormat(recordFormat);
// Open the file for updating
myFile.open(AS400File.READ_WRITE, 0, AS400File.COMMIT_LOCK_LEVEL_NONE);
// Update the first record in the file. Assume
// that newName is a String with the new name for
// CUSTNAME
Record updateRec = myFile.readFirst();
updateRec.setField("CUSTNAME", newName);
myFile.update(updateRec);
....
// 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 class="section"><p>Use the <a href="javadoc/com/ibm/as400/access/AS400File.html#WRITE(COM.IBM.AS400.ACCESS.RECORD)"> write()</a> method to append records to the
end of a file. A single record or an array of records can be appended to the
file.</p>
</div>
<div class="section"><p>Use the <a href="javadoc/com/ibm/as400/access/AS400File.html#DELETECURRENTRECORD()"> deleteCurrentRecord()</a> method to delete
the record at the cursor position.</p>
</div>
</div>
</body>
</html>