ProblemYou want an easy way to access your relational database. SolutionUse an implementation of the Java Data Objects specification.
DiscussionAs mentioned, JDO provides easy access to databases. JDO is a specification from Sun; there are many implementations of it. JDO works by inserting extra code into your data classes, a process it calls " enhancement." The extra code is what transparently interfaces with the database. The JDO spec is not tied to the relational model; JDO data can be stored in a local file, a relational database, an object database, or whatever else an implementation chooses to use. The "Reference Implementation" from Sun uses a file store on local disks. Example 20-1 is the Serialization program from Chapter 10 (Recipe 10.18) implemented using JDO. Recall from that discussion that the save( ) method in the parent class calls the write( ) method implemented in this subclass to save a variety of objects. Example 20-1. SerialDemoJDO/** * A demonstration of serialization using JDO. */ public class SerialDemoJDO extends SerialDemoAbstractBase { public static void main(String[] args) throws IOException { SerialDemoJDO jd = new SerialDemoJDO( ); jd.save( ); jd.dump( ); } public PersistenceManager getPM( ) { Properties p = new Properties( ); try { p.load(new FileInputStream("jdo.properties")); PersistenceManagerFactory pmf = JDOHelper.getPersistenceManagerFactory(p); return pmf.getPersistenceManager( ); } catch (IOException ex) { throw new RuntimeException(ex.toString( )); } } public void write(Object o) { PersistenceManager pm = getPM( ); pm.currentTransaction( ).begin( ); if (o instanceof Collection) { pm.makePersistentAll((Collection)o); } else { pm.makePersistent(o); } pm.currentTransaction( ).commit( ); pm.close( ); } public void dump( ) { PersistenceManager pm = getPM( ); Object[] data = new Object[3]; pm.retrieveAll(data); for (int i = 0; i < data.length; i++) { System.out.println(data[i]); } pm.close( ); } } Two things are not shown here: how the MyData class used in the earlier recipe gets turned into a JDO class and how JDO knows how and where to store the data. The Enhancement process requires an XML configuration file to tell it which classes are to be enhanced that is, which are the data classes. This, and the actual class files, are read by the JDO enhancer, and new class files are generated. For example, a setName( ) method that simply saves its argument in a field might be enhanced to also send it as an update to a database. The enhancement process consists of running an enhancer program provided with your implementation; for the Sun Reference Implementation, this can be as simple as: java com.sun.jdori.enhancer.Main -s build/classdir build/com/darwinsys/Mydata.class Naturally there is an Ant task for automating this step. To tell JDO where to store its data, a properties file is loaded, which specifies a minimum of four parameters: the Service Provider to use, the database name, and a user name and password. The Reference Implementation provides an FOStore class that saves file objects on the local disk. A set of properties for this implementation could be the following: javax.jdo.PersistenceManagerFactoryClass=com.sun.jdori.fostore.FOStorePMF javax.jdo.option.ConnectionURL=fostore:database/javasrc.io # For the JDO RI, it doesn't matter what name/passwd you use, but # both MUST be specified as some value. javax.jdo.option.ConnectionUserName=ian javax.jdo.option.ConnectionPassword=anything Once all these pieces are in place, the program in Example 20-1 can be run. The MyData objects are saved to disk and displayed by the program. Where JDO really comes into its own is that it will just as easily access an SQL database, assuming that your JDO implementation supports this. All that is necessary is to extend the jdo.properties file to have a JDBC driver and a JDBC-style URL (details of these are discussed in Recipe 20.5). For example: javax.jdo.option.ConnectionURL=jdbc:postgresql:ecom javax.jdo.option.ConnectionDriverName=org.postgresql.Driver See AlsoThere is much more to JDO than I've covered here. The web site http://www.jdocentral.com/ contains JDO information, articles, and links to many implementations. The O'Reilly book Java Data Objects, by David Jordan and Craig Russell (both of whom are on the JDO standards committee), covers JDO in detail. |