home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


DBDemoApplet reads and writes a persistent object called TNurse to the database. TNurse is defined in TNurse.java, given in Listing 37.11.

Listing 37.11 TNurse.java—Be Sure to Use ODI’s osjcfp Utility to Make This Class Persistent


import java.util.*;
import COM.odi.*;
import COM.odi.util.*;

public class TNurse
{
  private String fID;
  private String fLastName;   

  // Has table of all nurses
  private static OSHashtable all;
       
  private TNurse(String theID, String theLastName)
  {
    fID = theID;
    fLastName = theLastName;
    all.put(fID, this);
  }   

  static public TNurse add(String theID, String theLastName)
⇒throws Exception
  {
    if (all.containsKey(theID, true))
      throw new Exception(“Nurse ID “ + theID + “ already exists.”);
    else
      return (new TNurse(theID, theLastName));
 }
      
       static TNurse get(String theID) throws Exception
       {
               TNurse theResult = (TNurse)all.get(theID);
               if (theResult == null) {
                      throw new Exception(“Nurse “ + theID + “ not found.”);
               }
               return theResult;
       }
      
       public String toString()
       {
               return (“ID: “ + fID + “; Name: “+ fLastName);
       }
      
       static void initialize(Database db)
       {
                try {
                System.out.println(“Looking up nurses.”);
                                  System.out.println(“N1.Database open is “ + db.isOpen());
                all = (OSHashtable)db.getRoot(“Nurses”);
                                  System.out.println(“N2.Database open is “ + db.isOpen());
               } catch (DatabaseRootNotFoundException e) {
                       System.out.println(“Making new root.”);
                       all = new OSHashtable();
                       db.createRoot(“Nurses”, all);
               }
        }
       
        public String getID()
        {
               return fID;
        }
       
        public String getLastName()
        {
               return fLastName;
        }
       
        static public Enumeration getAll()
        {
        return all.elements();
        }
       
        // This class is not to be used as a persistent hash key
        public int hashCode()
        {
               return super.hashCode();
       }
   

  public static void main(String args[])
  {       
    System.out.println(“TNurse class running.”);
    String host = System.getProperty(“COM.odi.host”);
    String dbpath = System.getProperty(“demo.dbpath”);

    if (dbpath == null)
      dbpath = “demo.odb”;
   
    ObjectStore.initialize(host, null);
    Database db = Database.open(dbpath, ObjectStore.UPDATE);
   
    Transaction t = Transaction.begin(ObjectStore.UPDATE);
    TNurse.initialize(db);
     
    System.out.println(“Adding Nurse Jones”);
    TNurse nurseJones = null;
    try {
      nurseJones = TNurse.add(“12345A”, “Jones”);
      System.out.println(“Nurse Jones is “ + nurseJones.toString());
    } catch (Exception e) {
        System.out.println(“Exception: “ + e);
        return;
    }
   
    TNurse nurseJonesAgain = null;
    try {
        nurseJonesAgain = TNurse.add(“12345A”, “Jones”);
    } catch (Exception e) {
        System.out.println(“Exception: “ + e); //expect duplicate
    }
    System.out.println(“Nurse Jones is “ + nurseJones.toString());
    System.out.println(“Nurse Jones again is “ +
⇒nurseJonesAgain.toString());
   
    t.commit();
    db.close();
    ObjectStore.shutdown(true);
    System.out.println(“TNurse class finished.”);
  }
}

Figure 37.13 shows DBDemoApplet in action.


NOTE:  In order to read and write data on the local hard drive, an applet must be “trusted.” Your browser will ask you if you want to trust this applet—you should answer “Yes.”


FIGURE 37.13  DBDemoApplet reads and writes data to an ObjectStore database.

HelloApplet as a Standalone Application

Recall that our applet example (HelloApplet) ran in a Graphical User Interface (GUI) provided by the Web browser. You don’t need to limit your Java GUI programming to writing applets that run only inside of Web pages—you can write a standalone application with its own graphical environment.

It is often more appropriate—or more convenient—to write a standalone Java application. A Servlet—a Java program that runs on your Web server—is one example of a Java application; it runs in the environment of a Web server such as Sun’s Java Server. The HelloGUI program described here is another example.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.