Project

General

Profile

« Previous | Next » 

Revision 775

Added by bojilova over 23 years ago

rename the DB* classes to:
AbstractDatabase
OracleAdapter
PostgresqlAdapter
SqlserverAdapter

View differences:

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
   * When record is inserted in the table and before insert trigger
43
   * gets a nextval from that sequence, select currval of that
44
   * sequence can return the generated key in the same db connection.
45
   *
46
   * @param conn db connection in which the unique id was generated
47
   * @param tableName the name of table which unique id was generate
48
   * @exception SQLException <br/> any SQLException that can be thrown 
49
   *            during the db operation
50
   * @return return the generated unique id as a long type
51
   */
52
  public long getUniqueID(Connection conn, String tableName) 
53
                                         throws SQLException {
54
    long uniqueid = 0;
55
    Statement stmt = conn.createStatement();
56
    stmt.execute("SELECT " + tableName + "_id_seq.currval FROM dual");
57
    ResultSet rs = stmt.getResultSet();
58
    if ( rs.next() ) {
59
      uniqueid = rs.getLong(1);
60
    }
61
    stmt.close();
62
    //System.out.println("Unique ID: " + uniqueid);    
63
    return uniqueid;
64
  }
65

  
66
  /**
67
   * The Oracle's function name that gets the current date and time
68
   * from the database server: "sysdate"
69
   *
70
   * @return return the current date and time function name: "sysdate"
71
   */
72
  public String getDateTimeFunction() {
73

  
74
    //System.out.println("The date and time function: " + "sysdate");    
75
    return "sysdate";
76
  }
77

  
78
  /**
79
   * The Oracle's function name that is used to return non-NULL value
80
   *
81
   * @return return the non-NULL function name: "nvl"
82
   */
83
  public String getIsNULLFunction() {
84
    
85
    return "nvl";
86
  }
87

  
88
  /**
89
   * The Oracles's string delimiter character: single quote (')
90
   *
91
   * @return return the string delimiter: single quote (')
92
   */
93
  public String getStringDelimiter() {
94

  
95
    return "'";
96
  }
97
  
98
}
99
    
100 0

  
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 the unique id was generated
54
   * @param tableName the table which unique id was 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
  /**
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
  public abstract String getDateTimeFunction();
69

  
70
  /**
71
   * The function name that is used to return non-NULL value
72
   *
73
   * @return return the non-NULL function name
74
   */
75
  public abstract String getIsNULLFunction();
76

  
77
  /**
78
   * The character that the specific database implementation uses to 
79
   * indicate string literals in SQL. This will usually be a single
80
   * qoute (').
81
   *
82
   * @return return the string delimiter
83
   */
84
  public abstract String getStringDelimiter();
85
  
86
  /**
87
   * Instantiate a class using the name of the class at runtime
88
   *
89
   * @param className the fully qualified name of the class to instantiate
90
   */
91
  static public Object createObject(String className) throws Exception {
92
 
93
    Object object = null;
94
    try {
95
      Class classDefinition = Class.forName(className);
96
      object = classDefinition.newInstance();
97
    } catch (InstantiationException e) {
98
      throw e;
99
    } catch (IllegalAccessException e) {
100
      throw e;
101
    } catch (ClassNotFoundException e) {
102
      throw e;
103
    }
104
    return object;
105
  }
106

  
107
  /**
108
   * the main routine used to test the DBAdapter utility.
109
   */
110
  static public void main(String[] args) {
111
    
112
    // Determine our db adapter class and
113
    // create an instance of that class
114
    try {
115
      MetaCatUtil util = new MetaCatUtil();
116
      String dbAdapter = util.getOption("dbAdapter");
117
      DBAdapter dbAdapterObj = (DBAdapter)createObject(dbAdapter);
118
      
119
      // test if they work correctly
120
      String date = dbAdapterObj.getDateTimeFunction();
121

  
122
      Connection conn = util.openDBConnection();
123
      long uniqueid = dbAdapterObj.getUniqueID(conn, "xml_catalog");
124
      conn.close();
125
      conn = null;
126

  
127
    } catch (Exception e) {
128
      System.out.println(e);
129
    }
130
  }
131
  
132
}
133
    
134 0

  
src/edu/ucsb/nceas/dbadapter/DBPostgresql.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A db adapter class for PostgreSQL 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 PostgreSQL db adapter implementation.
34
 */
35
public class DBPostgresql extends DBAdapter {
36

  
37
  /**
38
   * The PostgreSQL unique ID /sequence generator
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
   * When record is inserted in the table and before insert trigger
43
   * gets a nextval from that sequence, select currval of that
44
   * sequence can return the generated key in the same db connection.
45
   *
46
   * @param conn db connection in which the unique id was generated
47
   * @param tableName the name of table which unique id was generate
48
   * @exception SQLException any SQLException that can be thrown 
49
   *            during the db operation
50
   * @return return the generated unique id as a long type
51
   */
52
  public long getUniqueID(Connection conn, String tableName) 
53
                                         throws SQLException {
54
    long uniqueid = 0;
55
    Statement stmt = conn.createStatement();
56
    stmt.execute("SELECT currval('" + tableName + "_id_seq')");
57
    ResultSet rs = stmt.getResultSet();
58
    if ( rs.next() ) 
59
		{
60
      uniqueid = rs.getLong(1);
61
    }
62
    stmt.close();
63
    //System.out.println("Unique ID: " + uniqueid);    
64
    return uniqueid;
65
  }
66

  
67
  /**
68
   * The PostgreSQL function name that gets the current date 
69
   * and time from the database server
70
   *
71
   * @return return the current date and time function name: "now()"
72
   */
73
  public String getDateTimeFunction() {
74

  
75
    //System.out.println("The date and time function: " + "now()");    
76
		//to return just the date use now()::date
77
		//and for the time use now()::time
78
    return "now()";
79
  }
80

  
81
  /**
82
   * The PostgreSQL function name that is used to return non-NULL value
83
   *
84
   * @return return the non-NULL function name: "coalesce"
85
   */
86
  public String getIsNULLFunction() {
87
    
88
    return "coalesce";
89
  }
90

  
91
  /**
92
   * PostgreSQL's string delimiter character: single quote (')
93
   *
94
   * @return return the string delimiter: single quote (')
95
   */
96
  public String getStringDelimiter() {
97

  
98
    return "'";
99
  }
100
  
101
}
102
    
103 0

  
src/edu/ucsb/nceas/dbadapter/DBSqlServer.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A db adapter class for MS SQL Server 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 MS SQL Server db adapter implementation.
34
 */
35
public class DBSqlServer extends DBAdapter {
36

  
37
  /**
38
   * The SQL Server unique ID generator through use of IDENTITY key
39
   * The IDENTITY key is a column in the table. When record is inserted
40
   * in the table, SELECT @@IDENTITY can return the key generated in
41
   * that IDENTITY column in the same db connection.
42
   * This is the only way to get unique id: let the SQL Server assign
43
   * a value in IDENTITY column and get it afterwards for use in the 
44
   * application.
45
   *
46
   * @param conn db connection in which to generate the unique id
47
   * @param tableName the name of table which unique id to generate
48
   * @exception SQLException <br/> any SQLException that can be thrown 
49
   *            during the db operation
50
   * @return return the generated unique id as a long type
51
   */
52
  public long getUniqueID(Connection conn, String tableName) 
53
                                         throws SQLException {
54
    long uniqueid = 0;
55
    Statement stmt = conn.createStatement();
56
    stmt.execute("SELECT @@IDENTITY");
57
    ResultSet rs = stmt.getResultSet();
58
    if ( rs.next() ) {
59
      uniqueid = rs.getLong(1);
60
    }
61
    stmt.close();
62
    //System.out.println("Unique ID: " + uniqueid);    
63
    return uniqueid;
64
  }
65

  
66
  /**
67
   * The SQL Server's function name that gets the current date and time
68
   * from the database server: "getdate()"
69
   *
70
   * @return return the current date and time function name: "getdate()"
71
   */
72
  public String getDateTimeFunction() {
73

  
74
    //System.out.println("The date and time function: " + "getdate()");    
75
    return "getdate()";
76
  }
77

  
78
  /**
79
   * The SQL Server's function name that is used to return non-NULL value
80
   *
81
   * @return return the non-NULL function name: "isnull"
82
   */
83
  public String getIsNULLFunction() {
84
    
85
    return "isnull";
86
  }
87

  
88
  /**
89
   * The SQL Server's string delimiter character: single quote (')
90
   *
91
   * @return return the string delimiter: single quote (')
92
   */
93
  public String getStringDelimiter() {
94

  
95
    return "'";
96
  }
97
  
98
}
99
    
100 0

  
src/edu/ucsb/nceas/dbadapter/PostgresqlAdapter.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An adapter class for PostgreSQL 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 PostgreSQL db adapter implementation.
34
 */
35
public class PostgresqlAdapter extends AbstractDatabase {
36

  
37
  /**
38
   * The PostgreSQL unique ID /sequence generator
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
   * When record is inserted in the table and insert trigger gets
43
   * a nextval from that sequence, select currval of that sequence
44
   * can return the generated key in the same db connection.
45
   *
46
   * @param conn db connection in which the unique id was generated
47
   * @param tableName the name of table which unique id was generate
48
   * @exception SQLException any SQLException that can be thrown 
49
   *            during the db operation
50
   * @return return the generated unique id as a long type
51
   */
52
  public long getUniqueID(Connection conn, String tableName) 
53
                                         throws SQLException {
54
    long uniqueid = 0;
55
    Statement stmt = conn.createStatement();
56
    stmt.execute("SELECT currval('" + tableName + "_id_seq')");
57
    ResultSet rs = stmt.getResultSet();
58
    if ( rs.next() ) 
59
		{
60
      uniqueid = rs.getLong(1);
61
    }
62
    stmt.close();
63
    //System.out.println("Unique ID: " + uniqueid);    
64
    return uniqueid;
65
  }
66

  
67
  /**
68
   * The PostgreSQL function name that gets the current date 
69
   * and time from the database server
70
   *
71
   * @return return the current date and time function name: "now()"
72
   */
73
  public String getDateTimeFunction() {
74

  
75
    //System.out.println("The date and time function: " + "now()");    
76
		//to return just the date use now()::date
77
		//and for the time use now()::time
78
    return "now()";
79
  }
80

  
81
  /**
82
   * The PostgreSQL function name that is used to return non-NULL value
83
   *
84
   * @return return the non-NULL function name: "coalesce"
85
   */
86
  public String getIsNULLFunction() {
87
    
88
    return "coalesce";
89
  }
90

  
91
  /**
92
   * PostgreSQL's string delimiter character: single quote (')
93
   *
94
   * @return return the string delimiter: single quote (')
95
   */
96
  public String getStringDelimiter() {
97

  
98
    return "'";
99
  }
100
  
101
}
102
    
0 103

  
src/edu/ucsb/nceas/dbadapter/OracleAdapter.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An 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 OracleAdapter extends AbstractDatabase {
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
   * When record is inserted in the table and insert trigger gets
43
   * a nextval from that sequence, select currval of that sequence
44
   * can return the generated key in the same db connection.
45
   *
46
   * @param conn db connection in which the unique id was generated
47
   * @param tableName the name of table which unique id was generate
48
   * @exception SQLException <br/> any SQLException that can be thrown 
49
   *            during the db operation
50
   * @return return the generated unique id as a long type
51
   */
52
  public long getUniqueID(Connection conn, String tableName) 
53
                                         throws SQLException {
54
    long uniqueid = 0;
55
    Statement stmt = conn.createStatement();
56
    stmt.execute("SELECT " + tableName + "_id_seq.currval FROM dual");
57
    ResultSet rs = stmt.getResultSet();
58
    if ( rs.next() ) {
59
      uniqueid = rs.getLong(1);
60
    }
61
    stmt.close();
62
    //System.out.println("Unique ID: " + uniqueid);    
63
    return uniqueid;
64
  }
65

  
66
  /**
67
   * The Oracle's function name that gets the current date and time
68
   * from the database server: "sysdate"
69
   *
70
   * @return return the current date and time function name: "sysdate"
71
   */
72
  public String getDateTimeFunction() {
73

  
74
    //System.out.println("The date and time function: " + "sysdate");    
75
    return "sysdate";
76
  }
77

  
78
  /**
79
   * The Oracle's function name that is used to return non-NULL value
80
   *
81
   * @return return the non-NULL function name: "nvl"
82
   */
83
  public String getIsNULLFunction() {
84
    
85
    return "nvl";
86
  }
87

  
88
  /**
89
   * The Oracles's string delimiter character: single quote (')
90
   *
91
   * @return return the string delimiter: single quote (')
92
   */
93
  public String getStringDelimiter() {
94

  
95
    return "'";
96
  }
97
  
98
}
99
    
0 100

  
src/edu/ucsb/nceas/dbadapter/SqlserverAdapter.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An adapter class for MS SQL Server 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 MS SQL Server db adapter implementation.
34
 */
35
public class SqlserverAdapter extends AbstractDatabase {
36

  
37
  /**
38
   * The SQL Server unique ID generator through use of IDENTITY key
39
   * The IDENTITY key is a column in the table. When record is inserted
40
   * in the table, SELECT @@IDENTITY can return the key generated in
41
   * that IDENTITY column in the same db connection.
42
   * This is the only way to get unique id: let the SQL Server assign
43
   * a value in IDENTITY column and get it afterwards for use in the 
44
   * application.
45
   *
46
   * @param conn db connection in which to generate the unique id
47
   * @param tableName the name of table which unique id to generate
48
   * @exception SQLException <br/> any SQLException that can be thrown 
49
   *            during the db operation
50
   * @return return the generated unique id as a long type
51
   */
52
  public long getUniqueID(Connection conn, String tableName) 
53
                                         throws SQLException {
54
    long uniqueid = 0;
55
    Statement stmt = conn.createStatement();
56
    stmt.execute("SELECT @@IDENTITY");
57
    ResultSet rs = stmt.getResultSet();
58
    if ( rs.next() ) {
59
      uniqueid = rs.getLong(1);
60
    }
61
    stmt.close();
62
    //System.out.println("Unique ID: " + uniqueid);    
63
    return uniqueid;
64
  }
65

  
66
  /**
67
   * The SQL Server's function name that gets the current date and time
68
   * from the database server: "getdate()"
69
   *
70
   * @return return the current date and time function name: "getdate()"
71
   */
72
  public String getDateTimeFunction() {
73

  
74
    //System.out.println("The date and time function: " + "getdate()");    
75
    return "getdate()";
76
  }
77

  
78
  /**
79
   * The SQL Server's function name that is used to return non-NULL value
80
   *
81
   * @return return the non-NULL function name: "isnull"
82
   */
83
  public String getIsNULLFunction() {
84
    
85
    return "isnull";
86
  }
87

  
88
  /**
89
   * The SQL Server's string delimiter character: single quote (')
90
   *
91
   * @return return the string delimiter: single quote (')
92
   */
93
  public String getStringDelimiter() {
94

  
95
    return "'";
96
  }
97
  
98
}
99
    
0 100

  
src/edu/ucsb/nceas/dbadapter/AbstractDatabase.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
 * <dbname>Adapter that extends edu.ucsb.nceas.dbadapter.AbstarctDatabase
44
 * (where dbname is the name of the database or database driver you wish
45
 * to add to your application). AbstarctDatabase is an abstract class,
46
 * thus the subclasses need to implement the abstract methods.
47
 */
48
public abstract class AbstractDatabase {
49

  
50
  /**
51
   * Unique ID generator
52
   *
53
   * @param conn db connection in which the unique id was generated
54
   * @param tableName the table which unique id was 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
  /**
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
  public abstract String getDateTimeFunction();
69

  
70
  /**
71
   * The function name that is used to return non-NULL value
72
   *
73
   * @return return the non-NULL function name
74
   */
75
  public abstract String getIsNULLFunction();
76

  
77
  /**
78
   * The character that the specific database implementation uses to 
79
   * indicate string literals in SQL. This will usually be a single
80
   * qoute (').
81
   *
82
   * @return return the string delimiter
83
   */
84
  public abstract String getStringDelimiter();
85
  
86
  /**
87
   * Instantiate a class using the name of the class at runtime
88
   *
89
   * @param className the fully qualified name of the class to instantiate
90
   */
91
  static public Object createObject(String className) throws Exception {
92
 
93
    Object object = null;
94
    try {
95
      Class classDefinition = Class.forName(className);
96
      object = classDefinition.newInstance();
97
    } catch (InstantiationException e) {
98
      throw e;
99
    } catch (IllegalAccessException e) {
100
      throw e;
101
    } catch (ClassNotFoundException e) {
102
      throw e;
103
    }
104
    return object;
105
  }
106

  
107
  /**
108
   * the main routine used to test the dbadapter utility.
109
   */
110
  static public void main(String[] args) {
111
    
112
    // Determine our db adapter class and
113
    // create an instance of that class
114
    try {
115
      MetaCatUtil util = new MetaCatUtil();
116
      String dbAdapter = util.getOption("dbAdapter");
117
      AbstractDatabase dbAdapterObj = (AbstractDatabase)createObject(dbAdapter);
118
      
119
      // test if they work correctly
120
      String date = dbAdapterObj.getDateTimeFunction();
121

  
122
      Connection conn = util.openDBConnection();
123
      long uniqueid = dbAdapterObj.getUniqueID(conn, "xml_catalog");
124
      conn.close();
125
      conn = null;
126

  
127
    } catch (Exception e) {
128
      System.out.println(e);
129
    }
130
  }
131
  
132
}
133
    
0 134

  

Also available in: Unified diff