Berikut beberapa listing untuk penggunaan Object Database Persistent dalam praktikum OODB.


CLASS Barang.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import org.garret.perst.*;
 public class Barang extends Persistent {
 String KodeBarang;
 String NamaBarang;
 String Jenis;
 double HargaSatuan;
 String Supplier;
 public Barang(String kd,String nm, String jns, double hrg, String sup){
 this.KodeBarang = kd;
 this.NamaBarang = nm;
 this.Jenis =jns;
 this.HargaSatuan =hrg;
 this.Supplier = sup;
 }
 public String getKodeBarang(){
 return KodeBarang;
 }
 public String getNamaBarang(){
 return NamaBarang;
 }
 public String getJenis(){
 return Jenis;
 }
 public double getHarga(){
 return HargaSatuan;
 }
 public String getSupplier(){
 return Supplier;
 }
 public String toString(){
 return KodeBarang+"/"+NamaBarang+"/"+Jenis+"/"+
 String.valueOf(HargaSatuan)+"/"+Supplier;
 }
 }


CLASS Main.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;


 import java.io.File;
import org.garret.perst.*;
 import java.util.*;
public class Main {
 public static void main(String[] args) {
 new File("Barang.dbs").delete();
 Storage db = StorageFactory.getInstance().createStorage();
 db.open("Barang.dbs");

 IPersistentSet root = (IPersistentSet)db.getRoot();
 if (root == null) {
 root = db.createSet();
 db.setRoot(root);
 }
 root.add(new Barang("M33",
 "Mie Goreng Ayam","Mie",1200,"Fastfood"));
 root.add(new Barang("S03",
 "Sun Silk","Shampo",6500,"UniClean"));
 root.add(new Barang("M11",
 "Mie Goreng Ayam","Mie",1000,"FlyFood"));
 root.add(new Barang("K31",
 "Kopi cap API","Kopi",2700,"Obor"));
 root.add(new Barang("T01",
"Teh Aroma Wangi","Teh",4500,"UniClean"));
 root.add(new Barang("M02",
 "Soto Mie","Mie",1100,"Fastfood"));
 root.add(new Barang("M51",
 "Mie Kari","Mie",1300,"FlyFood"));
root.add(new Barang("K21",
 "Kopi cap Wangi","Kopi",7500,"Uniclean"));
 root.add(new Barang("T05",
 "Teh T-Melati","Teh",5500,"Obor"));
 root.add(new Barang("M82",
 "Mie Bakso","Mie",1200,"Fastfood"));

 System.out.println("Object sudah persistent dalam Database");
 Iterator iterator = root.iterator();
 while (iterator.hasNext()) {
 System.out.println(iterator.next());
 }
 db.close(); }
 }






Keterangan:
Sebelumnya harus me-load libraries yang berisi garret –dspace –perst.
Jika tidak maka akan error.

Output: