1   package ch.ige.edossier.util;
2   
3   import java.sql.*;
4   import javax.sql.DataSource;
5   import javax.naming.Context;
6   import javax.naming.InitialContext;
7   import java.util.Properties;
8   import org.apache.log4j.Logger;
9   
10  /**
11   * B32.03 - eDossier-Interceptions - Diplomarbeit an der Software-Schule Schweiz<br>
12   * Die Klasse DBHelper ist eine Hilfsklasse, welche die Connection auf die Datenbank erstellt
13   * und dies wieder schliesst.
14   * <p>
15   * Copyright (c) 2004, Eidgenössisches Institut für Geistiges Eigentum
16   * @author    Anita Rueegsegger, Marc Bouquet
17   * @version   $Id: DBHelper.java,v 1.10 2004/11/17 02:55:44 ruegsegger Exp $
18   */
19  public class DBHelper
20  {
21    // Attribute für Log4j-Logging
22    private static final Logger LOG = Logger.getLogger( DBHelper.class );
23  
24    private static DBHelper dbHelper = null; // stat. Feld für Singleton
25  
26  
27    //ABNAHME BAGIS DB
28    public static String DBBAGIS = null;
29    public static String DRIVERBAGIS = null;
30    public static String USERBAGIS = null;
31    public static String PWBAGIS = null;
32  
33    public static String DBJIPPS = null;
34    public static String DRIVERJIPPS = null;
35    public static String USERJIPPS = null;
36    public static String PWJIPPS = null;
37  
38    /** Konstruktor - Singleton. */
39    private DBHelper()
40    {
41      try
42      {
43        Properties properties = ServerProperties.loadServerProperties();
44  
45        this.DBBAGIS = properties.getProperty( "dbbagis" );
46        this.DRIVERBAGIS = properties.getProperty( "dirverbagis" );
47        this.USERBAGIS = properties.getProperty( "userbagis" );
48        this.PWBAGIS = properties.getProperty( "pwbagis" );
49  
50        this.DBJIPPS = properties.getProperty( "dbjipps" );
51        this.DRIVERJIPPS = properties.getProperty( "driverjipps" );
52        this.USERJIPPS = properties.getProperty( "userjipps" );
53        this.PWJIPPS = properties.getProperty( "pwjipps" );
54      }
55      catch( Exception ex )
56      {
57        LOG.fatal( "Fehler: " + ex.getMessage() );
58      }
59    }
60  
61    /**
62     * statische Methode, die eine Referenz auf den DBHelper
63     * zurückgibt. Hier ist das Singleton-Pattern implementiert.
64     * @return Referenz auf DBHelper (= me)
65     */
66    public synchronized static DBHelper getInstance()
67    {
68      if( dbHelper == null )
69      {
70        dbHelper = new DBHelper();
71      }
72      return dbHelper;
73    }
74  
75    /**
76     * Mit der Methode getConnection wird die Verbindung auf die Datenbank erstellt
77     * @return Connection
78     */
79    public synchronized Connection getConnection()
80    {
81      Connection con = null;
82  
83      try
84      {
85        // Driver registieren
86        Class.forName( DRIVERBAGIS );
87  
88        // Connect zur Datenbank ausführen
89        con = DriverManager.getConnection( DBBAGIS, USERBAGIS, PWBAGIS );
90      }
91      catch( Exception ex )
92      {
93        LOG.error( "Exception: " + ex.getMessage() );
94        ex.printStackTrace();
95      }
96      return con;
97    }
98  
99    /**
100    * Mit der Methode getConnection wird die Verbindung auf die Datenbank erstellt
101    * @return Connection
102    */
103   public synchronized Connection getJippsConnection()
104   {
105     Connection con = null;
106 
107     try
108     {
109       // Driver registieren
110       Class.forName( DRIVERJIPPS );
111 
112       // Connect zur Datenbank ausführen
113       con = DriverManager.getConnection( DBJIPPS, USERJIPPS, PWJIPPS );
114     }
115     catch( Exception ex )
116     {
117       LOG.error( "Exception: " + ex.getMessage() );
118       ex.printStackTrace();
119     }
120     return con;
121   }
122 
123   /**
124    * Mit der Methode getConnection wird die Verbindung auf die Datenbank erstellt
125    * @return Connection
126    */
127   public synchronized Connection getEsolutionConnection()
128   {
129     Connection con = null;
130 
131     try
132     {
133       Context initContext = new InitialContext();
134       Context envContext  = (Context)initContext.lookup("java:/comp/env");
135       DataSource ds = (DataSource)envContext.lookup("jdbc/DIPLOMDB");
136       con = ds.getConnection();
137     }
138     catch( Exception ex )
139     {
140       LOG.error( "Exception: " + ex.getMessage() );
141       ex.printStackTrace();
142     }
143     return con;
144   }
145 
146 
147 
148   /**
149    * Schliesst die Connection und das Statement
150    * @param con Connection
151    * @param stmt Statement
152    * @throws SQLException Unerwarterer SQL-Fehler
153    */
154   public void close( Connection con, Statement stmt ) throws SQLException
155   {
156     try
157     {
158       if( stmt != null )
159       {
160         stmt.close();
161       }
162       if( con != null )
163       {
164         con.close();
165       }
166     }
167     catch( SQLException sqlex )
168     {
169       LOG.warn( "SQLException: " + sqlex.getMessage() );
170       throw new SQLException( "Connection kann nicht geschlossen werden:" + sqlex.getMessage() );
171     }
172   }
173 
174   /**
175    * Schliesst die Connection und das PreparedStatement
176    * @param con Connection
177    * @param ps PreparedStatement
178    * @throws SQLException Unerwarteter SQL-Fehler
179    */
180   public void close( Connection con, PreparedStatement ps ) throws SQLException
181   {
182     try
183     {
184       if( ps != null )
185       {
186         ps.close();
187       }
188       if( con != null )
189       {
190         con.close();
191       }
192     }
193     catch( SQLException sqlex )
194     {
195       LOG.warn( "SQLException: " + sqlex.getMessage() );
196       throw new SQLException( "Connection kann nicht geschlossen werden:" + sqlex.getMessage() );
197     }
198   }
199 }
200