Hello World!!
|
 |
Description:
This quintessential helloworld program shows the basics of creating a federated database and a schema, and includes basic application that accesses the database. The application creates a database, creates a container in the database, adds objects in the container, and then retrieves these objects via a name lookup.
Download:
main.java
import com.objy.db.DatabaseNotFoundException;
import com.objy.db.DatabaseOpenException;
import com.objy.db.ObjyRuntimeException;
import com.objy.db.app.Connection;
import com.objy.db.app.Session;
import com.objy.db.app.oo;
import com.objy.db.app.ooDBObj;
import com.objy.db.app.ooFDObj;
/**
* Main driver class
*
* @author Brian Clark
* @version 1.0
*/
public class Main
{
/**
* Objectivity/DB HelloWorld example to store a simple HelloWorld object in
* the database, and then retrieve it.
*
* @param args if specified should be a relative path to the federated
* database bootstrap file.
*/
public static void main(String[] args)
{
boolean errors = true;
try
{
System.out.println("Welcome to the Objectivity HelloWorld Example");
// default assumes running from Samples/java/helloWorld folder
String bootfile = "../../data/helloWorld/HelloWorld.boot";
if (args.length == 0)
{
System.out.println(
"No Objectivity FD Bootfile passed as argument; using default: " +
bootfile);
}
else
{
bootfile = args[0];
System.out.println("Using FD Bootfile passed as argument: " + bootfile);
}
//Open a read/write Connection to the Objectivity Federated Database
//and catch the standard exceptions.
Connection connection;
try
{
connection = Connection.open(bootfile, oo.openReadWrite);
}
catch (DatabaseNotFoundException e1)
{
System.out.println("Federated database not found " + bootfile +
"- use oonewfd to create federated database.");
return;
}
catch (DatabaseOpenException e2)
{
System.out.println("Federated database already open.");
return;
}
//Create a new Objectivity Session with client side cache
//dimensions of at most and at least 200 pages (Page size
//defaults to 8K on NT).
Session session;
session = new Session(20, 20);
String dbName = "TestDB";
session.setOpenMode(oo.openReadWrite);
session.begin();
ooFDObj fd = session.getFD();
ooDBObj db;
//if the database already exists, then delete it
if (fd.hasDB(dbName))
{
System.out.println("Database " + dbName +
" exists; will delete and re-create");
db = fd.lookupDB(dbName);
db.delete();
}
System.out.println("Create database");
//create a new database file within the open federation
try
{
db = fd.newDB(dbName);
}
catch (ObjyRuntimeException e3)
{
System.out.println("Database " + dbName +
" could not be created--check free disk space.");
session.abort();
return;
}
//
// create HelloObject and set message
//
System.out.println("Create HelloObject and set message");
String name = "HelloWorld";
HelloObject hello;
hello = new HelloObject();
hello.setHelloMsg(name);
//
// and name it in the scope of the database
//
db.nameObj(hello, name);
//
// commit the transaction
//
session.commit();
//
// begin a read transaction
//
session.begin();
System.out.println("Looking for object with name " + name);
//
// lookup the object with name "HelloWorld" in the scope of the database
//
HelloObject hello2;
try
{
hello2 = (HelloObject)db.lookupObj(name);
System.out.println("Found it!: " + hello2.getHelloMsg());
//
// commit the transaction
//
session.commit();
//
// everything worked so far.
//
errors = false;
}
catch (ObjyRuntimeException e4)
{
System.out.println("HelloObject not found: will abort");
session.abort();
errors = true;
}
}
finally
{
System.out.println("");
if (errors)
{
System.out.println("HelloWorld test has FAILED");
}
else
{
System.out.println("HelloWorld test has PASSED");
}
}
}
}
helloWorld.java
import com.objy.db.app.ooObj;
// Class HelloObject represents the HelloWorld object
public class HelloObject extends ooObj
{
/** String to contain the message */
private String mHelloMsg;
// constructor
public HelloObject()
{
}
// sets the helloMsg value to the provided value
void setHelloMsg(String value)
{
markModified();
mHelloMsg = value;
}
// returns the current helloMsg value
String getHelloMsg()
{
fetch();
return mHelloMsg;
}
}
;