Java Proxies for Database Objects
by Paul Lipton


Example 1:
public DBObject createDBObject(String classFamilyName,
                                String className) throws DatabaseException


Listing One
// Machine generated
package jp.jasmine.CAStore;

import jp.jasmine.japi.*;
import java.math.BigDecimal;
import java.sql.Date;
import java.rmi.*;
import java.util.*;

public class Customer  extends Person {
   protected static DBClass dbc;
   public Customer(Database db, String oid) throws DatabaseException {
      super(db,oid);
   }
   public Customer() throws DatabaseException {
   }
   public static Customer createCustomer(Database db) 
                                           throws DatabaseException {
      Hashtable arg = new Hashtable();

      DBObject dbo = db.createDBObject("CAStore", "Customer", arg);
      return (Customer) toApplicationObject(dbo);
   }
   private static void setDBClass(Database db) throws DatabaseException {
      if (db==null) throw new 
                       DatabaseException("Null database in setDBClass()");
      if (dbc==null) dbc = db.getDBClass("CAStore", "Customer");
   }
   // -------------------- Properties --------------------
   public int getCustomernumber() throws DatabaseException {
      return (dbo==null) ? 0 : dbo.getIntProperty("customernumber");
   }
   public void setCustomernumber(int customernumber) 
                                            throws DatabaseException {
      if (dbo!=null) dbo.setProperty("customernumber", customernumber);
   }
   // -------------------- Methods -----------------------
   public static DBCollection getLowCreditRisks( Database db ) 
                                              throws DatabaseException {
      if (dbc==null) setDBClass(db);
      Object arg[] = {};
      Object o = null;
      if(dbc!=null)
         o=dbc.execMethod("getLowCreditRisks", arg, 
                                              "List<CAStore::Customer>");
      return (DBCollection) o;
   }
}


Listing Two
import jp.jasmine.japi.*;
import java.io.*;

public class example2
{
  public static void main(String args[]) {
    try {
        Database db;
        // Connect to database as 3-tier application and start a session.
        db=new Database("pcl.cai.com", 1099);
        db.startSession();
        db.startTransaction();

        // Create a database object and proxy it in one call.
        DBObject dbo = db.createDBObject("CAStore", "Customer");

        db.endTransaction();
        db.endSession();
    }
    // Simple exception handling.
    catch( Exception ex) {
        System.out.println("test error: " + ex.toString());
        ex.printStackTrace();
    } 
  }
}

Listing Three
import jp.jasmine.japi.*;
import java.io.*;
import java.util.*;

public class example3
{
  public static void main(String args[]) {
    try {
        int aChar;
        String customerName;
        Database db;
        DBObject dbo;

        db=new Database("pcl.cai.com", 1099);   // 1099 is the port number
        db.startSession();
        db.startTransaction();

        // Create a proxy for new temporary database collection of customers.
        DBCollection dblist = 
                db.createDBCollection("List", "CAStore::Customer", null);
        // Proxy the customer class (CAStore is like a Jasmine "package").
        DBClass dbclass = db.getDBClass("CAStore", "Customer");
        
        // Call a server-side method to query, call credit bureaus, etc.
        dblist = (DBCollection) dbclass.execMethod("getLowCreditRisks", 
                                          null, "Bag <CAStore::Customer>");
        // Print name of each low credit risk customer & dump photo to disk.
        Enumeration e = dblist.elements();
        for (; e.hasMoreElements(); ) {
            dbo = (DBObject) e.nextElement();
            customerName = (String) dbo.getProperty("name");
            dbo = (DBObject) dbo.getProperty("photo");
            if (dbo == null) 
                System.out.println("Name: " + customerName + 
                                                 " image not available!");
            else {

                System.out.println("Name: " + customerName + "  File: " 
                                                  + customerName + ".bmp");
                // Stream the multimedia object that we proxy to disk.
                InputStream is = dbo.getInputStream();
                OutputStream fos = new BufferedOutputStream(new 
                                  FileOutputStream(customerName + ".bmp"));
                while((aChar = is.read()) >= 0) 
                    fos.write(aChar);
                fos.close();
                is.close();
            }
        }
        db.endTransaction();
        db.endSession();
    }
    // Simple exception handling.
    catch( Exception ex) {
        System.out.println("test error: " + ex.toString());
        ex.printStackTrace();
    } 
  }
}





3


