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
19 public class DBHelper
20 {
21 private static final Logger LOG = Logger.getLogger( DBHelper.class );
23
24 private static DBHelper dbHelper = null;
26
27 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
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
66 public synchronized static DBHelper getInstance()
67 {
68 if( dbHelper == null )
69 {
70 dbHelper = new DBHelper();
71 }
72 return dbHelper;
73 }
74
75
79 public synchronized Connection getConnection()
80 {
81 Connection con = null;
82
83 try
84 {
85 Class.forName( DRIVERBAGIS );
87
88 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
103 public synchronized Connection getJippsConnection()
104 {
105 Connection con = null;
106
107 try
108 {
109 Class.forName( DRIVERJIPPS );
111
112 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
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
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
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