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