ObjectDB - Object Database for Java Home | About Us | Customers | Free Download | Purchase
 
  <Back to Main Menu>   
 
 
JDO Directory Demo
Demo Home

Source Code
directory.Utilities
directory.pc.*
directory.step1.*
directory.step2.*
directory.step3.*
directory.step4.*

J2SDK Instructions
J2SDK on Windows
J2SDK on Unix

IDE Instructions
Borland JBuilder
IBM Eclipse
NetBeans / Forte /
Sun One Studio
JCreator
 



 

ObjectDB for Java/JDO Demo
class directory.Utilities

     JDO Directory

The directory.Utilities class contains a single static method, getPersistenceManager(...), which is used by all the demo steps to obtain a PersistenceManager instance, representing a connection to the database.

directory/Utilities.java
 // ObjectDB for Java Demo - JDO Directory - A Brief JDO Tutorial
 // Copyright (C) 2001-2003, ObjectDB Software. All rights reserved.
 
 package directory;
 
 import java.io.*;
 import java.util.Properties;
 import javax.jdo.*;
 
 /**
  * General JDO Utilities
  */
 public class Utilities {
     
     /**
      * Obtains a PersistenceManager representing a database connection.
      *
      * @param clean indicates if a clean new database is preferred
      */
     public static PersistenceManager getPersistenceManager(boolean clean) {
         
         try {
             // Prepare the jdo.properties file for reading:
             InputStream in =
                 Utilities.class.getResourceAsStream("jdo.properties");
 
             try {
                 // Load the properties from the file:
                 Properties properties = new Properties();
                 properties.load(in);
                 
                 // If requested - try to delete an old local odb file:
                 if (clean) {
                     String path = (String)properties.get(
                         "javax.jdo.option.ConnectionURL");
                     if (!path.startsWith("objectdb://")) // only local
                         new File(path).delete();
                 }
                 
                 // Obtain a PersistenceManagerFactory and a PersistenceManager:
                 PersistenceManagerFactory pmf =
                     JDOHelper.getPersistenceManagerFactory(
                         properties, JDOHelper.class.getClassLoader());
                 return pmf.getPersistenceManager();
             }
             finally {
                 in.close();   
             }
         }
         
         // Handle errors:
         catch (IOException x) {
             throw new RuntimeException("Error reading jdo.properties");
         }
     }
 }
 

The connection details are provided in a jdo.properties resource file that is loaded by the getPersistenceManager(...) method above.

directory/jdo.properties
 # ObjectDB PMF class:
 javax.jdo.PersistenceManagerFactoryClass=com.objectdb.jdo.PMF
 
 # Connection URL for embedded mode:
 javax.jdo.option.ConnectionURL=directory.odb
 # TODO: Comment the above line to use client-server mode
 
 # Connection URL for client-server mode:
 #javax.jdo.option.ConnectionURL=objectdb://localhost/directory2.odb
 # TODO: Uncomment the above line to use client-server mode
 
 # User Details (for client-server mode):
 javax.jdo.option.ConnectionUserName=admin
 javax.jdo.option.ConnectionPassword=admin
 
 
 




Copyright 2001-2007 ObjectDB Software. All rights reserved.