Berkeley DB Java Edition Examples

# Berkeley DB Java Edition
Examples

The JE distribution comes with examples that illustrate:

- using the JE programming interfaces to perform basic database operations
- scenarios from the Getting Started Guide and Writing Transactional Applications
- using the Direct Persistence Layer to implement common SQL queries
- using JE High Availability

Building and running JE examples
Basic Example
Getting Started Examples
Writing Transactional Applications Examples
Translating SQL Queries
Examples List

### Building and Running a Basic Example

Compiling and running a simple example can serve as a sanity check of the installation. Follow the instructions below to compile and run the PersonExample.

You can find the source for this example at:

>     JE_HOME/examples/persist/PersonExample.java

Assuming you have installed the JavaSE JDK and have verified that you have a working Java compiler, you can build `PersonExample` as follows.

1. Change to the

       JE_HOME/examples

   directory.
2. Set your CLASSPATH to include both

       JE_HOME/lib/je-M.N.P.jar

   and the

       JE_HOME/examples

   directory.
3. Compile `PersonExample.java` with the following command:

       javac persist/PersonExample.java

   or on Windows:

       javac persist\PersonExample.java

To run `PersonExample` , use the following command, specifying an environment directory for the data generated by the example:

>     java persist.PersonExample -h <environment directory>

For example, using `"."` for the second parameter will write the database files into the current directory. You'll notice that a `00000000.jdb` file and and `je.lck` file are created. This is the first log file in the environment and a lock file. If you need to delete the environment for running a different example, simply delete these two files.

When you run the program you'll see the following output. While this is not a very entertaining program, it is enough to test that you have installed JE correctly.

    222-22-2222 Jack Smith
    333-33-3333 Mary Smith

The other JE examples are compiled and run similarly. How to run the examples from the Getting Started Guide and Writing Transactional Applications is described in the sections below, as well as how to run the Translating SQL Queries examples. Instructions for running other examples are contained in the example's source file.

### Running the Getting Started Examples

As described in the Berkeley DB Java Edition Getting Started Guide, the final examples in every chapter exist in the JE package. You can build and run these examples as follows:

- Change to the

      JE_HOME/examples

  directory.
- Set your CLASSPATH to include both

      JE_HOME/lib/je-M.N.P.jar

  and the

      JE_HOME/examples

  directories.
- Compile the Getting Started examples with the following commands:

      javac je/gettingStarted/*.java

  or on Windows:

      javac je\gettingStarted\*.java

- Make a directory to contain your database environment:

      mkdir gsgEnv

- Load your sample database:

      java je.gettingStarted.ExampleDatabasePut -h gsgEnv -i je/gettingStarted/inventory.txt  -v je/gettingStarted/vendors.txt

- Perform queries against your database. To see everything in the database, use:

      java je.gettingStarted.ExampleInventoryRead -h gsgEnv

  To perform a query based on an inventory item's name, use:

      java je.gettingStarted.ExampleInventoryRead -h gsgEnv -s Upo

### Running the Writing Transactional Applications Examples

The examples in Writing Transactional Applications with Berkeley DB, Java Edition guide exist in the JE package. You can build and run these examples as follows:

- Change to the

      JE_HOME/examples

  directory.
- Set your CLASSPATH to include both

      JE_HOME/lib/je-M.N.P.jar

  and the

      JE_HOME/examples

  directories.
- Compile the Transactional Applications examples with the following commands:

      javac je/txn/*.java

  or on Windows:

      javac je\txn\*.java

- Make a directory to contain your database environment:

      mkdir txnEx

- Run the transactional example.

      java je.txn.TxnGuide -h txnEx

### Running the Translating SQL Query Examples

This example shows how some common SQL queries can be implemented using the Direct Persistence Layer. It's meant to help users who are more familiar with SQL translate those approaches to the DPL. These queries include:

Basic data retrieval:
  `SELECT * FROM tab ORDER BY col ASC;`
A prefix query:
  `SELECT * FROM tab WHERE col LIKE 'prefix%';`
A range query, where the data type of A (as well as B) may be an int, a float, a String, etc:
  `SELECT * FROM tab WHERE col gt;= A AND col <= B;`
An equi-join on a single primary database:
  `SELECT * FROM tab WHERE col1 = A AND col2 =B;`
An equi-join on two primary databases combined with a filtering on "t2.col2". Note that if "t2.col2" is a secondary key, the filtering does a index lookup. Otherwise the filtering is done through database scanning:
  `SELECT t1.* FROM table1 t1, table2 t2 WHERE t1.col1 = t2.col1 AND t2.col2 = A;`

You can build and run these examples as follows:

- Change to the

      JE_HOME/examples

  directory.
- Set your CLASSPATH to include both

      JE_HOME/lib/je-M.N.P.jar

  and the

      JE_HOME/examples

  directories.
- Compile the SQL query examples with the following commands:

      javac persist/sqlapp/*.java

  or on Windows:

      javac persist\sqlapp\*.java

- Make a directory to contain your database environment:

      mkdir sqlEnv

- Run the SQL query examples:

      java persist.sqlapp.SQLApp -h sqlEnv

  To delete database files after the example exits, do:

      java persist.sqlapp.SQLApp -h sqlEnv -d

### List of Examples

Example | Location | API | Description
--- | --- | --- | ---
Getting Started Guide | examples/persist/ gettingStarted | DPL | scenarios using the Direct Persistence Layer from the Getting Started Guide
Writing Transactional Applications | examples/persist/txn | DPL | scenarios using the Direct Persistence Layer from Writing Transactional Applications
Writing Transactional Applications | examples/je/txn | Base | scenarios using the Base API from Writing Transactional Applications
Translating SQL Queries | examples/persist/sqlApp | DPL | shows how some common SQL queries can be implemented using the Direct Persistence Layer
PersonExample | examples/persist | DPL | demonstrates basic use of the Direct Persistence Layer
ScalaPersonExample | examples/persist | DPL | demonstrates using JE with the Scala programming language
EventExample EventExampleDPL | examples/persist | DPL | contrasts the Base API and the Direct Persistence Layer with an example of storing event objects
CustomKeyOrderExample | examples/persist | DPL | shows how to use a Comparable to specify key order
DplDump | examples/persist | DPL | dumps objects stored using the Direct Persistence Layer in XML format
HelloDatabaseWorld | examples/collections/hello | Collections | trivial example using the Collections API
AccessExample | examples/collections/access | Collections | reimplementation of the Base API AccessExample using the Collections API
Shipments | examples/collections/ship | Collections | series of examples based on a shipment database
SimpleExample | examples/je | Base | does basic data insertion and retrieval
BindingExample | examples/je | Base | shows how to use com.sleepycat.bind to convert between Java objects and JE data records
SecondaryExample | examples/je | Base | illustrates the use of secondary indices
SequenceExample | examples/je | Base | demonstrates the use of Sequence objects
ToManyExample | examples/je | Base | shows how to use multi-key secondary indices to support many-many and one-many primary/secondary key relationships
MeasureInsertSize | examples/je | Base | inserts a given set of key/value pairs in order to measure the disk space consumed by a given data set
JCA | examples/jca | Base | shows how to use the J2EE Connector Architecture with JE
StockQuotes | examples/je/rep/quote | High Availability/Replication | shows how to use BDB JE High Availability

Copyright (c) 2002, 2010 Oracle and/or its affiliates. All rights reserved.