Subsections


Using the Java Binding

Although the C++ binding is more complete than the Java binding - essentially according to the administrative operations - the Java bindings allow to manipulate data without limitations.

Using the Java binding is very similar to the C++ binding. Writing a Java program that can create, retrieve, modify and delete person instances that are stored in an EYEDB database involves the following steps:

  1. generates the specific Person binding using the eyedbodl tool
  2. write the Java client program
  3. compile the generated binding and the client program
This example is located in the examples/GettingStarted subdirectory.


Generating the Java code

The Java code is generated from the ODL schema definition using the following command:

% eyedbodl --gencode=Java --package=person person.odl
The -package option is mandatory: this name will be used as the name of the Java package to which all generated Java classes will belong.

This command will generate a number of Java file in subdirectory person/, each generated file containing a Java class having the same name.

If you have a look to the files in sub-directory person, you will notice that the following classes have been generated:

  1. the class Address
  2. the class Database
  3. the class Employee
  4. the class Person
  5. the class set_class_Person_ref


A minimal client program

We are now going to write a minimal client program which will perform the following operations:

  1. initialize the EYEDB and person packages
  2. connect to the EYEDB server
  3. open a database
  4. creates two person instances and mary them
Here is the code for this minimal client:
//
// Persontest.java
//

import person.*;

class PersonTest {
  public static void main(String args[]) {

    // Initialize the eyedb package and parse the default eyedb options
    // on the command line
    String[] outargs = org.eyedb.Root.init("PersonTest", args);
     
    // Check that a database name is given on the command line
    int argc = outargs.length;
    if (argc != 1) {
        System.err.println("usage: java PersonTest dbname");
        System.exit(1);
    }

    try {
      // Initialize the person package
      person.Database.init();

      // Open the connection with the backend
      org.eyedb.Connection conn = new org.eyedb.Connection();

      // Open the database named outargs[0]
      person.Database db = new person.Database(outargs[0]);
      db.open(conn, org.eyedb.Database.DBRW);

      db.transactionBegin();
      // Create two persons john and mary
      Person john = new Person(db);
      john.setFirstname("john");
      john.setLastname("travolta");
      john.setAge(26);
     
      Person mary = new Person(db);
      mary.setFirstname("mary");
      mary.setLastname("stuart");
      mary.setAge(22);
     
      // Mary them ;-)
      john.setSpouse(mary);

      // Store john and mary in the database
      john.store(org.eyedb.RecMode.FullRecurs);

      john.trace();

      db.transactionCommit();
    }
    catch(org.eyedb.Exception e) { // Catch any eyedb exception
       e.print();
       System.exit(1);
    }
  }
}
To use this client, you must first compile it using a standard Makefile, as follows (replace «datadir» with the data directory, usually /usr/share):
include <<datadir>>/eyedb/Makefile.eyedb

all: PersonTest.class

person/Database.java: schema.odl
        $(EYEDB_ODL) --gencode=Java --package=person --output-dir=person $<

PersonTest.class: PersonTest.java person/Database.java
        CLASSPATH=$(EYEDB_CLASSPATH):. javac *.java person/*.java
Once compiled, you can execute the program as follows:
% CLASSPATH=. eyedbjrun PersonTest person_g
The eyedbjrun script is a helper script that wraps the call to the Java virtual machine with an appropriate CLASSPATH environment variable containing the path to eyedb.jar and passes the necessary options to the PersonTest class.

A few remarks about the Java code: The Java binding support both the standalone applications and the applets.

To have more information about the Java binding, please refer to the EYEDB Java binding manual.

EyeDB manual