cf99a86a35d70e93dfe879f69686343bde8ab26d
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 VeresultSetion 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
.DatabaseMetaData
;
14 import java
.sql
.DriverManager
;
15 import java
.sql
.ResultSet
;
16 import java
.sql
.SQLException
;
17 import java
.sql
.Statement
;
19 import org
.apache
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.database
.types
.IDatabaseType
;
28 abstract class CdmDataSourceBase
implements ICdmDataSource
{
29 private static final Logger logger
= Logger
.getLogger(CdmDataSourceBase
.class);
31 private static final int TIMEOUT
= 10;
32 private Connection connection
;
34 private Connection
getConnection() {
37 if(connection
!= null && connection
.isValid(TIMEOUT
)){
40 IDatabaseType dbType
= getDatabaseType().getDatabaseType();
41 String classString
= dbType
.getClassString();
42 Class
.forName(classString
);
43 String mUrl
= dbType
.getConnectionString(this);
44 return DriverManager
.getConnection(mUrl
, getUsername(), getPassword());
46 } catch (ClassNotFoundException e
) {
47 logger
.error("Database driver class could not be loaded\n" + "Exception: " + e
.toString());
48 } catch(SQLException e
) {
49 logger
.error("Problems with database connection\n" + "Exception: " + e
.toString());
55 * @see eu.etaxonomy.cdm.database.ICdmDataSource#testConnection()
57 public boolean testConnection() throws ClassNotFoundException
, SQLException
{
59 IDatabaseType dbType
= getDatabaseType().getDatabaseType();
60 String classString
= dbType
.getClassString();
61 Class
.forName(classString
);
62 String mUrl
= dbType
.getConnectionString(this);
63 Connection connection
= DriverManager
.getConnection(mUrl
, getUsername(), getPassword());
64 if (connection
!= null){
72 public Object
getSingleValue(String query
) throws SQLException
{
73 String queryString
= query
== null?
"(null)": query
;
74 ResultSet resultSet
= executeQuery(query
);
75 if (resultSet
== null || resultSet
.next() == false){
76 logger
.warn("No record returned for query " + queryString
);
79 if (resultSet
.getMetaData().getColumnCount() != 1){
80 logger
.warn("More than one column selected in query" + queryString
);
83 Object object
= resultSet
.getObject(1);
84 if (resultSet
.next()){
85 logger
.warn("Multiple results for query " + queryString
);
93 * Executes a query and returns the ResultSet.
94 * @return ResultSet for the query.
95 * @throws SQLException
98 public ResultSet
executeQuery (String query
) throws SQLException
{
105 Connection connection
= getConnection();
106 Statement statement
= connection
.createStatement();
107 resultSet
= statement
.executeQuery(query
);
114 * @return return code
117 public int executeUpdate (String sqlUpdate
) {
120 Connection connection
= null;
122 if (sqlUpdate
== null){
125 connection
= getConnection();
126 Statement statement
= connection
.createStatement();
127 result
= statement
.executeUpdate(sqlUpdate
);
129 } catch(SQLException e
) {
130 logger
.error("Problems when executing update\n " + sqlUpdate
+ " \n" + "Exception: " + e
);
136 * @see eu.etaxonomy.cdm.database.ICdmDataSource#getMetaData()
139 public DatabaseMetaData
getMetaData() {
140 Connection connection
= null;
142 connection
= getConnection();
143 return connection
.getMetaData();
144 } catch (SQLException e
) {
145 logger
.error("Could not get metadata for datasource", e
);
151 * @see eu.etaxonomy.cdm.database.ICdmDataSource#closeOpenConnections()
154 public void closeOpenConnections() {
156 if(connection
!= null && !connection
.isClosed()){
160 } catch (SQLException e
) {
161 logger
.error("Error closing the connection");