Refactored methods in VersionableDaoBase etc + added Query-By-Example for CdmEntityDa...
[cdmlib.git] / cdmlib-persistence / src / main / java / eu / etaxonomy / cdm / database / CdmDataSourceBase.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.cdm.database;
11
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;
17
18 import org.apache.log4j.Logger;
19
20 import eu.etaxonomy.cdm.database.types.IDatabaseType;
21
22 /**
23 * @author a.mueller
24 * @created 18.12.2008
25 * @version 1.0
26 */
27 abstract class CdmDataSourceBase implements ICdmDataSource {
28 private static final Logger logger = Logger.getLogger(CdmDataSourceBase.class);
29
30
31 private Connection getConnection() {
32
33 Connection mConn = null;
34 try {
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());
44 }
45 return mConn;
46 }
47
48
49 /* (non-Javadoc)
50 * @see eu.etaxonomy.cdm.database.ICdmDataSource#testConnection()
51 */
52 public boolean testConnection() throws DataSourceNotFoundException {
53
54 IDatabaseType dbType = getDatabaseType().getDatabaseType();
55 String classString = dbType.getClassString();
56 try {
57 Class.forName(classString);
58 String mUrl = dbType.getConnectionString(this);
59 Connection mConn = DriverManager.getConnection(mUrl, getUsername(), getPassword());
60 if (mConn != null){
61 return true;
62 }
63 } catch (ClassNotFoundException e) {
64 throw new DataSourceNotFoundException(e);
65 } catch (SQLException e) {
66 throw new DataSourceNotFoundException(e);
67 }
68 return false;
69 }
70
71
72 /**
73 * Executes a query and returns the ResultSet.
74 * @return ResultSet for the query.
75 */
76 public ResultSet executeQuery (String query) {
77
78 ResultSet rs;
79 try {
80 if (query == null){
81 return null;
82 }
83 Connection mConn = getConnection();
84 Statement mStmt = mConn.createStatement();
85 rs = mStmt.executeQuery(query);
86 return rs;
87 } catch(SQLException e) {
88 logger.error("Problems when executing query \n " + query + " \n" + "Exception: " + e);
89 return null;
90 }
91 }
92
93 /**
94 * Executes an update
95 * @return return code
96 */
97 public int executeUpdate (String sqlUpdate) {
98
99 int result;
100 try {
101 if (sqlUpdate == null){
102 return 0;
103 }
104 Connection mConn = getConnection();
105 Statement mStmt = mConn.createStatement();
106 result = mStmt.executeUpdate(sqlUpdate);
107 return result;
108 } catch(SQLException e) {
109 logger.error("Problems when executing update\n " + sqlUpdate + " \n" + "Exception: " + e);
110 return 0;
111 }
112 }
113 }