45
Library Version 12.1.6.0
A DPL Example
12/7/2015
Getting Started with JE
Page 59
ExampleDatabasePut.java
Our example reads inventory and vendor information from flat text files, encapsulates this
data in objects of the appropriate type, and then writes each object to an EntityStore.
To begin, we import the Java classes that our example needs. Most of the imports are related
to reading the raw data from flat text files and breaking them apart for usage with our data
classes. We also import classes from the JE package, but we do not actually import any classes
from the DPL. The reason why is because we have placed almost all of our DPL work off into
other classes, so there is no need for direct usage of those APIs here.
package persist.gettingStarted;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import com.sleepycat.je.DatabaseException;
Now we can begin the class itself. Here we set default paths for the on-disk resources that we
require (the environment home, and the location of the text files containing our sample data).
We also declare DataAccessor and MyDbEnv members. We describe these classes and show
their implementation in DataAccessor.java (page 58) and MyDbEnv (page 56).
public class ExampleDatabasePut {
private static File myDbEnvPath = new File("/tmp/JEDB");
private static File inventoryFile = new File("./inventory.txt");
private static File vendorsFile = new File("./vendors.txt");
private DataAccessor da;
// Encapsulates the environment and data store.
private static MyDbEnv myDbEnv = new MyDbEnv();
Next, we provide our usage() method. The command line options provided there are
necessary only if the default values to the on-disk resources are not sufficient.
private static void usage() {
System.out.println("ExampleDatabasePut [-h <env directory>]");
System.out.println(" [-i <inventory file>]");
System.out.println(" [-v <vendors file>]");
System.exit(-1);
}
Our main() method is also reasonably self-explanatory. We simply instantiate an
ExampleDatabasePut object there and then call its run() method. We also provide a top-
level try block there for any exceptions that might be thrown during runtime.