JDBManager is a java implementation of the DBManager API. This package was designed to simplify access to the CDF Calibration database, but it may be usedfor read access on any Oracle or Msql database.
This package allows the user to specify runs, versions, and components they are interested in , then access that data without learning the intricacies of SQL or the underlying database schema.
This API uses the following types of objects to accomplish this:
Some Simplifying Assumptions were made in the development of this package.
A Simple Example
The following java program illustrates the concept of Data Object, Key,Table,and Manager being used together to extract data.
To run this java program, see the environment and compilation example.
//This is a deconstruction of DBUtils/jExamples/jReadDB.java //for completeness, code that is unimportant to the discussion but //necessary to run the example is typeset in this color import java.sql.SQLException; import emitter.DictEntry; import jdbc.SQLEmitter; import key.CalibKey; import manager.ManagerCreationVisitor; import manager.DefaultDBMS; import manager.Table; import manager.TableManager; /** * Usage: java jReadDB//the Dictionary static String topDictName = "TESTDicts"; static public void main(String args[]) { int i,j; //favourite iterators //parse command line //the third arg (database_name) is passed to the DefaultDBMS which //builds a database connection using data in the iomap.txt file and //also handles the two optional arguments. DefaultDBMS defDB = null; try { if( args.length == 3 ) { defDB = new DefaultDBMS(args[2]); } else if( args.length == 4 ) { defDB = new DefaultDBMS(args[2], args[3]); } else if( args.length == 5 ) { defDB = new DefaultDBMS(args[2], args[3], args[4]); } else { //print a usage message System.err.println("Usage: java jDBManager run version database_nam e [jdbc_URL] [jdbc_driver]"); System.exit(1); } } catch (Exception e) { e.printStackTrace(); System.out.println(e); System.exit(2); } int run = Integer.parseInt(args[0],10); //total number of sets int version = Integer.parseInt(args[1],10); //group size of each set // Connect to the database try { defDB.createConnection(); System.out.println("Connected to the database."); } catch( Exception e ) { System.err.println(e); e.printStackTrace(); System.exit(4); } // //create the Managers navigating the dictionary // //now load the dictionary top entry DictEntry topDict = null; try { //create the Dictionary Class c = Class.forName(topDictName); topDict = (DictEntry)c.newInstance(); //default const System.out.println("jDBManager: top dictionary entry is class: " + topDictName); } catch (Exception e) { e.printStackTrace(); System.out.println(e); throw new Error("Class " + topDictName + " not found"); } //make a ManagerCreationVisitor ManagerCreationVisitor mcVisitor = new ManagerCreationVisitor(defDB, topDictName); //introduce the visitor to the dictionary topDict.accept(mcVisitor); //make the Manager TableManager[] iom = mcVisitor.makeTableManager("CMPChEl"); if (iom.length<1) throw new Error("No attributes found in tree generated from " + topDictNam e); // // now lets put some stuff into the (pre-existing!) CMPChEl tables // int iComp = 0; try { //make a table row CMPChEl ttable = new CMPChEl(); //make a container of row objects Table cont = new Table(ttable.getClass()); //make a key CalibKey qk = new CalibKey(run,version); //make manager perform io on the database table iom[0].doGet(qk, cont); System.out.println(cont.niceOutputFormat()); } catch (Exception e) { System.err.println(e); e.printStackTrace(); System.exit(7); } } //end of main [jdbc URL] [jdbc driver] * Sample program for the jDBManager API. * This program fills either ORACLE or MSQL nTables tables, with nRows CMPChEl * instances (or with whatever you prefer to provide) and then reads back some * of them. * The attribute (row) name and the component (subdetector) names are * defined as two static strings below. * The username and password to log into the DBMS can be defined as java propert ies * DefaultsDBMS class provides valid default values (on cdfsga) for all * these variables. * @version 0.0.1 20 April 1999 * @author Paolo Calafiura (PCalafiura@lbl.gov ) */ public class jReadDB { //the entry at the top of the dictionary //needed to construct a Dictionary, and then later a Manager //see TESTDicts.java //to see what actually gets instantiated