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


Listing 37.10 DBDemo.java—This Test Program Opens a Database and Reads and Writes Data Interactively


import java.awt.*;
import java.applet.*;
import java.util.Properties;
import java.util.Enumeration;
import COM.odi.*;
import COM.odi.util.*;

public class DBDemo extends Applet
{
    private Database fDb;
    private Transaction fTrans;

    // indicates that ObjectStore has been initialized
    private Thread fInitialized;
    private Session fSession;

        public void init()
        {
               // Take out this line if you don’t use
⇒symantec.itools.net.RelativeURL or
⇒symantec.itools.awt.util.StatusScroller
               symantec.itools.lang.Context.setApplet(this);

               // This code is automatically generated by Visual Cafe
⇒when you add
               // components to the visual environment.
⇒It instantiates and initializes
               // the components. To modify the code,
⇒only use code syntax that matches
               // what Visual Cafe can generate,
⇒or Visual Cafe may be unable to back
               // parse your Java file into its visual environment.
               //{{INIT_CONTROLS

               setLayout(null);
               setSize(426,400);
               theAddNurseLabel = new java.awt.Label(“Add Nurse to Database”);
               theAddNurseLabel.setBounds(48,192,360,32);
               theAddNurseLabel.setFont(new Font(“Dialog”, Font.BOLD, 18));
               add(theAddNurseLabel);
               theNursesInDatabaseLabel = new java.awt.Label(“Nurses in Database”);
               theNursesInDatabaseLabel.setBounds(24,12,360,32);
               theNursesInDatabaseLabel.setFont(new Font(“Dialog”, Font.BOLD, 18));
               add(theNursesInDatabaseLabel);
               theNurseIDLabel = new java.awt.Label(“ID”);
               theNurseIDLabel.setBounds(48,240,40,36);
               add(theNurseIDLabel);
               theNurseNameLabel = new java.awt.Label(“Last Name”);
               theNurseNameLabel.setBounds(48,276,72,54);
               add(theNurseNameLabel);
               theNurseIDField = new java.awt.TextField();
               theNurseIDField.setText(“the nurse ID”);
               theNurseIDField.setBounds(120,240,225,26);
               add(theNurseIDField);
               theNurseNameField = new java.awt.TextField();
               theNurseNameField.setText(“the nurse name”);
               theNurseNameField.setBounds(120,288,225,28);
               add(theNurseNameField);
               theAddButton = new java.awt.Button();
               theAddButton.setLabel(“Add”);
               theAddButton.setBounds(228,324,113,30);
               theAddButton.setFont(new Font(“Dialog”, Font.BOLD, 12));
               theAddButton.setBackground(java.awt.Color.lightGray);
               add(theAddButton);
               theNurseList = new java.awt.List(0,false);
               add(theNurseList);
               theNurseList.setBounds(36,60,329,103);
               //}}
             
               setupDatabase();
       
               //{{REGISTER_LISTENERS
               SymAction lSymAction = new SymAction();
               theAddButton.addActionListener(lSymAction);
               //}}
        }
       
        public void start()
        {
               reload(theNurseList, TNurse.getAll());
        }


        public void destroy() {
            // save everything to the database
            if (fDb == null) {
                System.out.println(“Database not open.”);
                return;
            }
            fTrans.commit();

            // and close the database
            if (fDb == null) {
                System.out.println(“No database to close.”);
                return;
            }
            fDb.close();
            fDb = null;
        }

        private void maybeInitialize() {
            if (fInitialized != null) {
                return;
            }
            Properties properties = new Properties();
            String user = getParameter(“user”);
            if (user != null) {
                properties.put(“COM.odi.user”, user);
            }
            String password = getParameter(“password”);
            if (password != null) {
                properties.put(“COM.odi.password”, password);
            }
            ObjectStore.initialize(null, properties);
            fInitialized = Thread.currentThread();
        }

        public String[][] getParameterInfo() {
             String pinfo[][] = {
               {“user”,    “String”,    “username for database”},
               {“password”, “String”, “password for database”}
         };
     return pinfo;
    }
   
    public void setupDatabase()
    {
    vvvv// Here begins the ObjectStore connection
                       fDb = null;
                       fTrans = null;
                       fInitialized = null;
                       fSession = null;

                       if (fDb != null) {
                   System.out.println(“Database already open.”);
                   return;
                      }

                      String theDbname = new String(“demo.odb”);

      // The following line starts a nonglobal session and joins this

      // thread to the new session. This allows the thread to use
      // the ObjectStore API.
                      Session.create(null, null).join();
      fSession = Session.getCurrent();
                      try {
                   //db = Database.create(dbname, 0664);
                   System.out.println(“Opening database”);
                   fDb = Database.open(theDbname, ObjectStore.OPEN_UPDATE);
                   System.out.println(“1a.Database open is “ + fDb.isOpen());

                  // Enable this code if you want a new database each time
                  /* db.destroy();
                        System.out.println(“Replacing database.”);
                         db = Database.create(dbname, ObjectStore.ALL_READ |
⇒ObjectStore.ALL_WRITE);
                   System.out.println(“1b.Database open is “ + db.isOpen());
                  */
                       } catch (AccessViolationException e) {
                   System.out.println(“File system access violation.”);
                   return;
                      } catch (DatabaseAlreadyExistsException e) {
                   System.out.println(“Database already exists.”);
                       } catch (DatabaseNotFoundException e) {
                                System.out.println(“Making new database.”);
                          fDb = Database.create(theDbname, ObjectStore.ALL_READ |
⇒ObjectStore.ALL_WRITE);
                      } catch (DatabaseException e) {
                  System.out.println(“Invalid database: “ + theDbname);
                  return;
                      }

                      if (fDb != null) {
                   fTrans = Transaction.begin(ObjectStore.UPDATE);
                   System.out.println(“Made new transaction.”);
                   System.out.println(“2.Database open is “ + fDb.isOpen());
                      }

                      System.out.println(“About to initialize TNurse”);
                      System.out.println(“3.Database open is “ + fDb.isOpen());
                      TNurse.initialize(fDb);
               }
              
               public void reload(java.awt.List theList, Enumeration e)
               {
                      // copy the elements of the enumeration into the list
                       for ( ; e.hasMoreElements();) {
                              theList.add((String) e.nextElement().toString());
                      }
               }
                          
    public static void main(String args[])
    {
        System.out.println(“Starting. . . “);
        DBDemo theApplet = new DBDemo();
        System.out.println(“setting up database”);
        theApplet.setupDatabase();
       
        System.out.println(“populating database”);
        try {
                        TNurse nurseSmith = TNurse.add(“123S”, “Smith”);
                        TNurse nurseJones = TNurse.add(“234J”, “Jones”);
                        TNurse nurseBrown = TNurse.add(“345B”, “Brown”);
                      } catch (Exception e) {
                              System.out.println(“Exception: “ + e);
                      }
                     
        System.out.println(“destroying”);
        theApplet.destroy();
        System.out.println(“done”);
        System.exit(0);
    }

        //{{DECLARE_CONTROLS
        java.awt.Label theAddNurseLabel;
        java.awt.Label theNursesInDatabaseLabel;
        java.awt.Label theNurseIDLabel;
        java.awt.Label theNurseNameLabel;
        java.awt.TextField theNurseIDField;
        java.awt.TextField theNurseNameField;
        java.awt.Button theAddButton;
        java.awt.List theNurseList;
        //}}

        class SymAction implements java.awt.event.ActionListener
        {
               public void actionPerformed(java.awt.event.ActionEvent event)
               {
                      Object object = event.getSource();
                      if (object == theAddButton)
                              theAddButton_Action(event);
               }
       }

        void theAddButton_Action(java.awt.event.ActionEvent event)
        {
               if (Session.getCurrent() == null)
      fSession.join();
     
    TNurse.initialize(fDb);
     
               String theID = theNurseIDField.getText();
               String theName = theNurseNameField.getText();
               try {
                 TNurse aNurse = TNurse.add(theID, theName);
               } catch (Exception e) {
                      System.out.println(“Exception: “ + e);
               }

               fTrans.commit();
          fTrans = Transaction.begin(ObjectStore.UPDATE);
          TNurse.initialize(fDb);
                      
               //{{CONNECTION
               // Clear the text for TextField
               theNurseIDField.setText(“”);
               //}}
                       
               //{{CONNECTION
               // Clear the text for TextField
               theNurseNameField.setText(“”);
               //}}
                       
               //{{CONNECTION
               // Clear the List
               theNurseList.removeAll();
               reload(theNurseList, TNurse.getAll());
               //}}
        }
}


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.