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:
The Java code is generated from the ODL schema definition using the following command:
% eyedbodl --gencode=Java --package=person person.odlThe -package option is mandatory: this name will be used as the name of the Java package to which all generated Java classes will belong.
If you have a look to the files in sub-directory person, you will notice that the following classes have been generated:
We are now going to write a minimal client program which will perform the following operations:
//
// 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_gThe 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.
EyeDB manual