Getting Started
Getting started with joodbms is much easier than you may expect. This guide will take you through:
- Importing the Joo libraries
- Creating a database
- Establishing a connection to a database
- Interacting with a database
This guide will NOT teach you about programming or object-oriented concepts.
Importing the Joo libraries
Every program that utilises the Joo library must first import it using the import keyword; this is done simply as follows:
import Joo.*;
In order for the import to work as expected you must add the Joo files to your project (otherwise where would it import Joo from?).
Creating a Database
If you are using Joo 1.0.0 then the database creation must be done manually; this is simpler than it sounds!
Step 1
Create a folder that will represent the database
Step 2
Create a file named “db.joo” in the database’s folder that has the following information:
Database Name
Database Description
Date Created
Each piece of data must be on a new line and must be in this same order.
Step 3
Create a new folder inside the database’s folder entitled “clusters”
Establishing a Database Connection
Method 1
Create a Joo object by passing the database location via the constructor in string format.
Joo dbConnection = new Joo(“/path/to/the/database/”);
Method 2
Create a Joo object using the default constructor and then call the connect method.
Joo dbConnection = new Joo();
if(dbConnection.connect(“/path/to/database/”))
{
System.out.println(“Connection established!”);
}
Creating a Cluster
A cluster in Joo is a group of stored objects that are referred to by a name specified by the developer (you). To create a cluster simply call the createCluster method; if the cluster was created the method will return true; if it fails for any reason (e.g. the cluster already exists) it will return false.
Joo db = new Joo("/path/to/db");
if(db.createCluster("example_cluster"))
{
System.out.println("The cluster was created!");
}
else
{
System.out.println("There was a problem creating the cluster!");
}
Storing an Object
Storing objects is done simply by calling the storeObject method which returns true upon successful insertion and false should anything go wrong (typically an IO error or an invalid cluster name being passed to the method).
If you wish to store an instance of a class you have written yourself, then you must implement the Serializable interface in your class. You are not required to implement any methods as the Serializable interface has no methods to implement, but is rather used for semantic purposes.
import Joo.*;
public class MyClass implements Serializable
{
private String foo;
public MyClass()
{
this.foo = "bar";
System.out.println("foo" + this.foo);
}
}
public class JooTest {
public static void main(String[] args) {
// Create a new Joo object and connect to the database
Joo db = new Joo();
System.out.println(db.connect("path to the database"));
// Create and store an object in the cluster named "cluster01"
MyClass objectToStore = new MyClass();
if(db.storeObject("cluster01", objectToStore))
{
System.out.println("The object was stored!");
}
else
{
System.out.println("There was a problem storing the object!");
}
}
}
Retrieving an Object
In order to retrieve an object from the database you must specify both the cluster it is stored in, and the OID of the object.
Should the method find the object it will return the object as type Object, you must then typecast this object to its original type, as can be seen in the example below.
Note: if no object is found then the method will return null.
import Joo.*;
public class JooTest {
public static void main(String[] args) {
// Create a new Joo object and connect to the database
Joo db = new Joo();
System.out.println(db.connect("path to the database"));
String objectFromDB;
objectFromDB = (String)db.getObject("example_cluster", 5);
}
}
