2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.database
;
12 import java
.sql
.Connection
;
13 import java
.sql
.DriverManager
;
14 import java
.sql
.ResultSet
;
15 import java
.sql
.SQLException
;
16 import java
.sql
.Statement
;
18 import org
.apache
.log4j
.Logger
;
20 import eu
.etaxonomy
.cdm
.database
.types
.IDatabaseType
;
27 abstract class CdmDataSourceBase
implements ICdmDataSource
{
28 private static final Logger logger
= Logger
.getLogger(CdmDataSourceBase
.class);
31 private Connection
getConnection() {
33 Connection mConn
= null;
35 IDatabaseType dbType
= getDatabaseType().getDatabaseType();
36 String classString
= dbType
.getClassString();
37 Class
.forName(classString
);
38 String mUrl
= dbType
.getConnectionString(this);
39 mConn
= DriverManager
.getConnection(mUrl
, getUsername(), getPassword());
40 } catch (ClassNotFoundException e
) {
41 logger
.error("Database driver class could not be loaded\n" + "Exception: " + e
.toString());
42 } catch(SQLException e
) {
43 logger
.error("Problems with database connection\n" + "Exception: " + e
.toString());
50 * @see eu.etaxonomy.cdm.database.ICdmDataSource#testConnection()
52 public boolean testConnection() throws DataSourceNotFoundException
{
54 IDatabaseType dbType
= getDatabaseType().getDatabaseType();
55 String classString
= dbType
.getClassString();
57 Class
.forName(classString
);
58 String mUrl
= dbType
.getConnectionString(this);
59 Connection mConn
= DriverManager
.getConnection(mUrl
, getUsername(), getPassword());
63 } catch (ClassNotFoundException e
) {
64 throw new DataSourceNotFoundException(e
);
65 } catch (SQLException e
) {
66 throw new DataSourceNotFoundException(e
);
73 * Executes a query and returns the ResultSet.
74 * @return ResultSet for the query.
76 public ResultSet
executeQuery (String query
) {
83 Connection mConn
= getConnection();
84 Statement mStmt
= mConn
.createStatement();
85 rs
= mStmt
.executeQuery(query
);
87 } catch(SQLException e
) {
88 logger
.error("Problems when executing query \n " + query
+ " \n" + "Exception: " + e
);
97 public int executeUpdate (String sqlUpdate
) {
101 if (sqlUpdate
== null){
104 Connection mConn
= getConnection();
105 Statement mStmt
= mConn
.createStatement();
106 result
= mStmt
.executeUpdate(sqlUpdate
);
108 } catch(SQLException e
) {
109 logger
.error("Problems when executing update\n " + sqlUpdate
+ " \n" + "Exception: " + e
);