|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
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.javaBe Sure to Use ODIs 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.
HelloApplet as a Standalone ApplicationRecall that our applet example (HelloApplet) ran in a Graphical User Interface (GUI) provided by the Web browser. You dont need to limit your Java GUI programming to writing applets that run only inside of Web pagesyou can write a standalone application with its own graphical environment. It is often more appropriateor more convenientto write a standalone Java application. A Servleta Java program that runs on your Web serveris one example of a Java application; it runs in the environment of a Web server such as Suns Java Server. The HelloGUI program described here is another example.
|
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. |