This example demonstrates the use of record level access.
[ Previous part ]
Use the following as an example for your program.
//////////////////////////////////////////////////////////////////////////////////
//
// Record level access example.
//
// Calling syntax: java RLACreateExample
//
//////////////////////////////////////////////////////////////////////////////////
import java.io.*;
import java.util.*;
import com.ibm.as400.access.*;
public class RLACreateExample
{
public static void main(String[] args)
{
AS400 system = new AS400(args[0]);
String filePathName = "/QSYS.LIB/MYLIB.LIB/MYFILE.FILE/MBR1.MBR"; Note 1
try
{
SequentialFile theFile = new SequentialFile(system, filePathName);
// Begin Note Two
CharacterFieldDescription lastNameField =
new CharacterFieldDescription(new AS400Text(20), "LNAME");
CharacterFieldDescription firstNameField =
new CharacterFieldDescription(new AS400Text(20), "FNAME");
BinaryFieldDescription yearsOld =
new BinaryFieldDescription(new AS400Bin4(), "AGE");
RecordFormat fileFormat = new RecordFormat("RF");
fileFormat.addFieldDescription(lastNameField);
fileFormat.addFieldDescription(firstNameField);
fileFormat.addFieldDescription(yearsOld);
theFile.create(fileFormat, "A file of names and ages"); Note 2
// End Note Two
theFile.open(AS400File.READ_WRITE, 1, AS400File.COMMIT_LOCK_LEVEL_NONE);
// Begin Note Three
Record newData = fileFormat.getNewRecord();
newData.setField("LNAME", "Doe");
newData.setField("FNAME", "John");
newData.setField("AGE", new Integer(63));
theFile.write(newData); Note 3
// End Note Three
theFile.close();
}
catch(Exception e)
{
System.out.println("An error has occurred: ");
e.printStackTrace();
}
system.disconnectService(AS400.RECORDACCESS);
System.exit(0);
}
}
[ Previous part ]