|
We build a Jeevan Database Application to illustrate the simplicity
and ease of use of Jeevan API.
We build a database for a computer manufacturing company. The database contains
information about computers and customers. The specific information we need
to store is as follows:
Computer: Serial number, model, price,
motherboard and repair history.
Motherboard: Serial number and model
Customer: Customer name and address
In addition to the above information, we need to maintain the link between the customer
and the computer purchased by the customer.
Design Application Classes
The first step is to design application classes. For this application, we need three
classes, the Computer, Motherboard and Customer. The
class definitions for these classes is given below:
//Define Motherboard class
public class Motherboard implements Serializable {
public String serialNumber;
public String model;
//constructor
public Motherboard(String sn, String m) {
serialNumber=sn;
model = m;
}
}
//Define Computer class
public class Computer implements Serializable {
public String serialNumber;
public String model;
public int price;
public Motherboard motherboard;
public String history;
//constructor
public Computer(String sn, String m, int p) {
serialNumber=sn;
model = m;
price = p;
history = null;
}
}
//Define Customer class
public class Customer implements Serializable {
public String name;
public String address;
//constructor
public Customer(String n, String a) {
name = n;
address = a;
}
}
Build Application
import com.w3apps.jeevan.*;
public class SimpleApplication {
public static void main (String arg[]) {
//1. start Jeevan Server
JeevanServer server = new JeevanServer();
//2. Create new database
Database db = server.createDatabase("ComputerDb");
//3. Define application classes
db.defineClass("Computer");
db.defineClass("Motherboard");
db.defineClass("Customer");
//4. Create first motherboard with serial number 'msn1' and model 'MM100'
Motherboard mboard1 = new MotherBoard("msn1", "MM100");
//5. Create first computer with serial number 'csn1' and model 'CM100'
// and price 2000 and with motherboard1
Computer computer1 = new Computer("csn1", "CM100", 2000);
computer1.motherboard = mboard1; //place motherboard in the computer
//6. Create second motherboard with serial number 'msn2' and model 'MM200'
Motherboard mboard1 = new MotherBoard("msn2", "MM200");
//7. Create second computer with serial number 'csn2' and model 'CM200'
// and price 2000 and with motherboard 2
Computer computer1 = new Computer("csn2", "CM200", 2000);
computer2.motherboard = mboard2;
//8. create new customers
Customer customer1 = new Customer("name1", "address1");
Customer customer2 = new Customer("name2", "address2");
//9. Store computers and customers in the database
database.insert(computer1);
database.insert(computer2);
database.insert(customer1);
database.insert(customer2);
//10. Link customer1 with computer1 and customer2 with compueter 2
customer1.relate(computer1);
customer2.relate(computer2);
/* We just created a new database and populated it with computer and customer
objects. We also related customers with computers. Next we retrieve objects
from the database
*/
//11. Select all computers from the database.
// ObjectSet contains all computer objects.
ObjectSet os = db.selectObjects("Computer");
//12. For each computer in the ObjectSet,print the computer serialNumbers.
// Increase the price of each computer by 100 and update the
database.
int count = os.getCount();
for (int i=0; i<count; i++) {
Computer comp = (Computer)os.getNext();
System.out.println(comp.serialNumber);
comp.price = comp.price + 100;
database.update(comp);
}
//13. Select Computer that has motherboard with serialNumber msn2
// and print its serial number.
ObjectSet os = database.select("Computer",
"motherboard.serialNumber == 'msn2' ");
//14. Get computer object purchased by customer1
Vector v = customer1.getRelatedObjects();
long objectId = v.elementAt(0);
Computer computer = (Computer)database.getObject(objectId);
System.out.println(computer.serialNumber);
}
You have just build a complete working Jeevan application.
The example above illustrates the ease and simplicity of building Jeevan application.
You do not need to pre compile your classes or need any database drivers such as
JDBC. For more information and in depth coverage of Jeevan APIs, download the Jeevan runtime version with a complete documentation.
|