Source code: org/enableit/db/DataSourceProxy.java
1 /*
2 * PROJECT : __PROJECT_NAME__
3 *
4 * COPYRIGHT : Copyright (C) 1999,2000,2001,2002 enableIT.org
5 *
6 * contact us at gpl@enableit.org.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22 package org.enableit.db;
23
24 // Java Imports
25 import java.util.*;
26 import java.sql.*;
27 import javax.naming.*;
28
29 // J2EE extensions
30 import javax.sql.*;
31
32 // Log4J Imports
33 import org.apache.log4j.Category;
34
35 /**
36 * Proxy class to wrap database access in a simple form. The user is required to
37 * provide their own <CODE>Connection</CODE>
38 *
39 * @version v1.2
40 *
41 * @author __AUTHOR__
42 *
43 */
44 public class DataSourceProxy
45 {
46
47 /**
48 * Get a connection from the named DataSource.
49 * This method is intended for use by this class itself, but may be useful
50 * to other objects, in that case care must be taken to close the
51 * <code>Connection</code> properly.
52 *
53 * @param dataSourceName
54 * The DataSource to lookup to obtain a connection
55 */
56 public static Connection getConnection(String dataSourceName)
57 throws DBException
58 {
59 cat.info("<ENTRY>");
60
61 Connection conn = null ;
62 InitialContext ic = null ;
63 try {
64 ic = new InitialContext() ;
65 // 1st try internal component name
66 Context envContext = (Context)ic.lookup("java:/comp/env") ;
67 DataSource ds = (DataSource)envContext.lookup(dataSourceName) ;
68 conn = ds.getConnection() ;
69 } catch (Exception e) {
70 // try as 'simple' JNDI name
71 try {
72 DataSource ds = (DataSource)ic.lookup(dataSourceName) ;
73 conn = ds.getConnection() ;
74 } catch (Exception e2) {
75 cat.error(e2.getMessage(), e2) ;
76 throw new DBException(e2.getMessage()) ;
77 }
78 }
79
80 cat.info("<EXIT>");
81 return conn ;
82 }
83
84 /**
85 * Execute the supplied SQL query against a connection
86 * obtained from the named DataSource.
87 *
88 * @param dataSourceName
89 * The datasource name to lookup in order to get a connection
90 *
91 * @param sql
92 * The SQL query to execute
93 *
94 * @return
95 * The SQL results stored as an <code>java.util.ArrayList</code>, each
96 * element of which is a <code>java.util.TreeMap</code> holding a
97 * single row of results
98 *
99 * @throws DBException
100 * If execution of the query failed.
101 */
102 public static java.util.List executeQuery(String dataSourceName, String sql)
103 throws DBException
104 {
105 cat.debug("<ENTRY>");
106
107 ArrayList results = null ;
108 Connection conn = null ;
109 try {
110 conn = DataSourceProxy.getConnection(dataSourceName) ;
111 results = DatabaseProxy.executeQuery(conn, sql) ;
112 } catch (Exception e) {
113 cat.error(e) ;
114 throw new DBException(e.getMessage()) ;
115 } finally {
116 try {
117 conn.close();
118 conn = null ;
119 } catch (Exception e) {
120 cat.fatal(e) ;
121 }
122 }
123
124 cat.debug("<EXIT>");
125 return results ;
126 }
127
128 /**
129 * Execute the supplied SQL update against a the supplied
130 * database connection.
131 *
132 * @param sql
133 * The SQL update to execute
134 *
135 * @param dataSourceName
136 * The datasource name to lookup in order to get a connection
137 *
138 * @return
139 * The number of rows affected by the update.
140 *
141 * @throws DBException
142 * If execution of the update failed.
143 */
144 public static int executeUpdate(String dataSourceName, String sql)
145 throws DBException
146 {
147 cat.debug("<ENTRY>");
148
149 int rowsAffected = 0 ;
150 Connection conn = null ;
151 try {
152 conn = DataSourceProxy.getConnection(dataSourceName) ;
153 rowsAffected = DatabaseProxy.executeUpdate(conn, sql) ;
154 } catch (Exception e) {
155 cat.error(e) ;
156 throw new DBException(e.getMessage()) ;
157 } finally {
158 try {
159 conn.close();
160 conn = null ;
161 } catch (Exception e) {
162 cat.fatal(e) ;
163 }
164 }
165
166 cat.debug("<EXIT>");
167 return rowsAffected;
168 }
169
170 /**
171 * Execute the supplied SQL update against a connection
172 * obtained from the named DataSource.
173 *
174 * @param dataSourceName
175 * The datasource name to lookup in order to get a connection
176 *
177 * @param sql
178 * The SQL update to execute with parameters represented by ?
179 *
180 * @param parms
181 * <CODE>ArrayList</CODE> containing parameters to insert in the sql statement
182 *
183 * @return
184 * The number of rows affected by the update.
185 *
186 * @throws DBException
187 * If execution of the update failed.
188 */
189 public static int executeUpdate(String dataSourceName, String sql, List parms)
190 throws DBException
191 {
192 cat.debug("<ENTRY>");
193
194 int rowsAffected = 0 ;
195 Connection conn = null ;
196 try {
197 conn = DataSourceProxy.getConnection(dataSourceName) ;
198 rowsAffected = DatabaseProxy.executeUpdate(conn, sql, parms) ;
199 } catch (Exception e) {
200 cat.error(e) ;
201 throw new DBException(e.getMessage()) ;
202 } finally {
203 try {
204 conn.close();
205 conn = null ;
206 } catch (Exception e) {
207 cat.fatal(e) ;
208 }
209 }
210
211 cat.debug("<EXIT>");
212 return rowsAffected;
213 }
214
215 /**
216 * Execute the supplied update procedure against a connection
217 * obtained from the named DataSource.
218 *
219 * @param dataSourceName
220 * The datasource name to lookup in order to get a connection
221 *
222 * @param sp
223 * A string containing the stored procedure name
224 *
225 * @param parms
226 * A <code>java.util.List</code> of String parameters for the stored procedure
227 *
228 * @return
229 * The number of rows affected by the stored procedure executed
230 *
231 * @throws DBException
232 * If execution of the stored procedure failed.
233 */
234 public static int executeDmlProcedure(String dataSourceName, String sp, List parms)
235 throws DBException
236 {
237 cat.debug("<ENTRY>");
238
239 int rowsAffected = 0 ;
240 Connection conn = null ;
241 try {
242 conn = DataSourceProxy.getConnection(dataSourceName) ;
243 rowsAffected = DatabaseProxy.executeUpdate(conn, sp, parms) ;
244 } catch (Exception e) {
245 cat.error(e) ;
246 throw new DBException(e.getMessage()) ;
247 } finally {
248 try {
249 conn.close();
250 conn = null ;
251 } catch (Exception e) {
252 cat.fatal(e) ;
253 }
254 }
255
256 cat.debug("<EXIT>");
257 return rowsAffected ;
258 }
259
260 /**
261 * Execute the supplied query procedure against a connection
262 * obtained from the named DataSource.
263 *
264 * @param dataSourceName
265 * The datasource name to lookup in order to get a connection
266 *
267 * @param sp
268 * A string containing the stored procedure name
269 *
270 * @param parms
271 * A <code>java.util.List</code> of String parameters for the stored procedure
272 *
273 * @return
274 * The SQL results stored as an <code>java.util.ArrayList</code>, each
275 * element of which is a <code>java.util.TreeMap</code> holding a
276 * single row of results
277 *
278 * @throws DBException
279 * If execution of the stored procedure failed.
280 */
281 public static java.util.List executeQueryProcedure(String dataSourceName, String sp, List parms)
282 throws DBException
283 {
284 cat.debug("<ENTRY>");
285
286 List results = null ;
287 Connection conn = null ;
288 try {
289 conn = DataSourceProxy.getConnection(dataSourceName) ;
290 results = (List)DatabaseProxy.executeQueryProcedure(conn, sp, parms, DatabaseProxy.LIST) ;
291 } catch (Exception e) {
292 cat.error(e) ;
293 throw new DBException(e.getMessage()) ;
294 } finally {
295 try {
296 conn.close();
297 conn = null ;
298 } catch (Exception e) {
299 cat.fatal(e) ;
300 }
301 }
302
303 cat.debug("<EXIT>");
304 return results ;
305 }
306
307
308 /**
309 * The Log4J <code>Category</code> doing the logging.
310 * Same <code>Category</code> is used throughout the library.
311 */
312 protected static Category cat = Category.getInstance(DataSourceProxy.class);
313
314 /**
315 * CVS info about this class and its current version
316 */
317 public static final String about = "$Id: DataSourceProxy.java,v 1.3 2003/01/06 10:34:50 TimAndVicky Exp $" ;
318
319 }
320