1 |
746
|
bojilova
|
/**
|
2 |
|
|
* '$RCSfile$'
|
3 |
|
|
* Purpose: An abstract class that encapsulates access to any RDBMS.
|
4 |
|
|
* This allows to swap easily between databases without any
|
5 |
|
|
* modification to the application.
|
6 |
|
|
* Copyright: 2000 Regents of the University of California and the
|
7 |
|
|
* National Center for Ecological Analysis and Synthesis
|
8 |
|
|
* Authors: Jivka Bojilova
|
9 |
|
|
* Release: @release@
|
10 |
|
|
*
|
11 |
|
|
* '$Author$'
|
12 |
|
|
* '$Date$'
|
13 |
|
|
* '$Revision$'
|
14 |
|
|
*
|
15 |
|
|
* This program is free software; you can redistribute it and/or modify
|
16 |
|
|
* it under the terms of the GNU General Public License as published by
|
17 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
18 |
|
|
* (at your option) any later version.
|
19 |
|
|
*
|
20 |
|
|
* This program is distributed in the hope that it will be useful,
|
21 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
22 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
23 |
|
|
* GNU General Public License for more details.
|
24 |
|
|
*
|
25 |
|
|
* You should have received a copy of the GNU General Public License
|
26 |
|
|
* along with this program; if not, write to the Free Software
|
27 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
package edu.ucsb.nceas.dbadapter;
|
31 |
|
|
|
32 |
|
|
import java.sql.*;
|
33 |
|
|
import edu.ucsb.nceas.metacat.MetaCatUtil;
|
34 |
|
|
|
35 |
|
|
/**
|
36 |
|
|
* Java offers uniform database access through the use of JDBC.
|
37 |
|
|
* But many databases still use different SQL implementations and
|
38 |
|
|
* conventions. Thus this class offers extended programming interface
|
39 |
|
|
* that all subclasses should implement to gain access to different
|
40 |
|
|
* databases.
|
41 |
|
|
*
|
42 |
|
|
* To add a new database adapter class you must create a new class
|
43 |
|
|
* DB<dbname> that extends edu.ucsb.nceas.dbadapter.DBAdapter (where
|
44 |
|
|
* dbname is the name of the database or database driver you wish to
|
45 |
|
|
* add to your application). DBAdapter is an abstract class, thus the
|
46 |
|
|
* subclasses need to implement the abstract methods.
|
47 |
|
|
*/
|
48 |
|
|
public abstract class DBAdapter {
|
49 |
|
|
|
50 |
|
|
/**
|
51 |
|
|
* Unique ID generator
|
52 |
|
|
*
|
53 |
757
|
bojilova
|
* @param conn db connection in which the unique id was generated
|
54 |
|
|
* @param tableName the table which unique id was generate
|
55 |
746
|
bojilova
|
* @exception SQLException <br/> any SQLException that can be thrown
|
56 |
|
|
* during the db operation
|
57 |
|
|
* @return return the generated unique id as a long type
|
58 |
|
|
*/
|
59 |
|
|
public abstract long getUniqueID(Connection conn, String tableName)
|
60 |
|
|
throws SQLException;
|
61 |
757
|
bojilova
|
|
62 |
746
|
bojilova
|
/**
|
63 |
|
|
* The function name that gets the current date and time
|
64 |
|
|
* from the database server
|
65 |
|
|
*
|
66 |
|
|
* @return return the current date and time function name
|
67 |
|
|
*/
|
68 |
753
|
harris
|
public abstract String getDateTimeFunction();
|
69 |
746
|
bojilova
|
|
70 |
|
|
/**
|
71 |
|
|
* The character that the specific database implementation uses to
|
72 |
|
|
* indicate string literals in SQL. This will usually be a single
|
73 |
|
|
* qoute (').
|
74 |
|
|
*
|
75 |
|
|
* @return return the string delimiter
|
76 |
|
|
*/
|
77 |
|
|
public abstract String getStringDelimiter();
|
78 |
|
|
|
79 |
|
|
/**
|
80 |
|
|
* Instantiate a class using the name of the class at runtime
|
81 |
|
|
*
|
82 |
|
|
* @param className the fully qualified name of the class to instantiate
|
83 |
|
|
*/
|
84 |
|
|
static public Object createObject(String className) throws Exception {
|
85 |
|
|
|
86 |
|
|
Object object = null;
|
87 |
|
|
try {
|
88 |
|
|
Class classDefinition = Class.forName(className);
|
89 |
|
|
object = classDefinition.newInstance();
|
90 |
|
|
} catch (InstantiationException e) {
|
91 |
|
|
throw e;
|
92 |
|
|
} catch (IllegalAccessException e) {
|
93 |
|
|
throw e;
|
94 |
|
|
} catch (ClassNotFoundException e) {
|
95 |
|
|
throw e;
|
96 |
|
|
}
|
97 |
|
|
return object;
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
/**
|
101 |
|
|
* the main routine used to test the DBAdapter utility.
|
102 |
|
|
*/
|
103 |
|
|
static public void main(String[] args) {
|
104 |
|
|
|
105 |
|
|
// Determine our db adapter class and
|
106 |
|
|
// create an instance of that class
|
107 |
|
|
try {
|
108 |
|
|
MetaCatUtil util = new MetaCatUtil();
|
109 |
|
|
String dbAdapter = util.getOption("dbAdapter");
|
110 |
|
|
DBAdapter dbAdapterObj = (DBAdapter)createObject(dbAdapter);
|
111 |
|
|
|
112 |
|
|
// test if they work correctly
|
113 |
753
|
harris
|
String date = dbAdapterObj.getDateTimeFunction();
|
114 |
746
|
bojilova
|
|
115 |
|
|
Connection conn = util.openDBConnection();
|
116 |
|
|
long uniqueid = dbAdapterObj.getUniqueID(conn, "xml_catalog");
|
117 |
|
|
conn.close();
|
118 |
|
|
conn = null;
|
119 |
|
|
|
120 |
|
|
} catch (Exception e) {
|
121 |
|
|
System.out.println(e);
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
}
|