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