View Javadoc
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.sql.SQLException; 25 import java.util.ArrayList; 26 import java.util.List; 27 28 import org.apache.log4j.Logger; 29 import org.enableit.db.beans.Provider; 30 31 32 /*** 33 * <p>Proxy class to wrap any java application database access in a simple form</p> 34 * 35 * @version v1.0 36 * 37 * @author __AUTHOR__ 38 * 39 */ 40 public class GenericDBProxy extends DatabaseProxy { 41 /*** 42 * The Log4J <code>Logger</code> doing the logging. 43 * Same <code>Logger</code> is used throughout the package. 44 */ 45 protected static Logger logger = Logger.getLogger(GenericDBProxy.class); 46 47 /*** 48 * Information on the exact CVS version accessible after compilation 49 */ 50 public static final String ABOUT = "$Revision: 1.11 $"; 51 52 /*** 53 * Execute the supplied SQL update query against a database connection 54 * supplied from <code>ConnectionFactory</code>. 55 * 56 * @param sql 57 * The SQL update to execute 58 * 59 * @return int 60 * The number of rows affected by the update. 61 * 62 * @throws DBException 63 * If execution of the update failed. 64 */ 65 public static int executeUpdate(String sql) 66 throws DBException { 67 logger.info("METHOD_ENTRY"); 68 69 int rowsAffected = 0; 70 71 Connection conn = null; 72 73 try { 74 // get connection, create statement and execute 75 conn = ConnectionFactory.getConnection(); 76 rowsAffected = executeUpdate(conn, sql); 77 } catch (DBException dbE) { 78 logger.error(dbE.getMessage()); 79 throw dbE; 80 } catch (Exception e) { 81 logger.error(e.getMessage()); 82 throw new DBException(e.getMessage()); 83 } finally { 84 try { 85 conn.close(); 86 conn = null; 87 } catch (Exception e) { 88 ; 89 } 90 } 91 92 logger.info("METHOD_EXIT"); 93 94 return rowsAffected; 95 } 96 97 /*** 98 * Execute the supplied SQL update query against a database connection 99 * supplied from <code>ConnectionFactory</code>. 100 * 101 * @param sql 102 * The SQL update to execute 103 * 104 * @return int 105 * The number of rows affected by the update. 106 * 107 * @throws DBException 108 * If execution of the update failed. 109 */ 110 public static int executeUpdate(Provider provider, String sql) 111 throws DBException { 112 logger.info("METHOD_ENTRY"); 113 114 int rowsAffected = 0; 115 116 Connection conn = null; 117 118 try { 119 // get connection, create statement and execute 120 conn = ConnectionFactory.getConnection(provider); 121 rowsAffected = executeUpdate(conn, sql); 122 } catch (DBException dbE) { 123 logger.error(dbE.getMessage()); 124 throw dbE; 125 } catch (Exception e) { 126 logger.error(e.getMessage()); 127 throw new DBException(e.getMessage()); 128 } finally { 129 try { 130 conn.close(); 131 conn = null; 132 } catch (Exception e) { 133 ; 134 } 135 } 136 137 logger.info("METHOD_EXIT"); 138 139 return rowsAffected; 140 } 141 142 /*** 143 * Execute the supplied SQL update query against a database connection 144 * supplied from <code>ConnectionFactory</code>. 145 * 146 * @param sql 147 * The SQL update to execute with parameters represented by ? 148 * 149 * @param parms 150 * <CODE>ArrayList</CODE> containing parameters to insert in the sql statement 151 * 152 * @return 153 * The number of rows affected by the update. 154 * 155 * @throws DBException 156 * If execution of the update failed. 157 */ 158 public static int executeUpdate(String sql, List parms) 159 throws SQLException, DBException { 160 logger.info("METHOD_ENTRY"); 161 162 int rowsAffected = 0; 163 164 Connection conn = null; 165 166 try { 167 // get connection, create statement and execute 168 conn = ConnectionFactory.getConnection(); 169 rowsAffected = executeUpdate(conn, sql, parms); 170 } catch (DBException dbE) { 171 logger.error(dbE.getMessage()); 172 throw dbE; 173 } catch (Exception e) { 174 logger.error(e.getMessage()); 175 throw new DBException(e.getMessage()); 176 } finally { 177 try { 178 conn.close(); 179 conn = null; 180 } catch (Exception e) { 181 ; 182 } 183 } 184 185 logger.info("METHOD_EXIT"); 186 187 return rowsAffected; 188 } 189 190 /*** 191 * Execute the supplied SQL update query against a database connection 192 * supplied from <code>ConnectionFactory</code>. 193 * 194 * @param sql 195 * The SQL update to execute with parameters represented by ? 196 * 197 * @param parms 198 * <CODE>ArrayList</CODE> containing parameters to insert in the sql statement 199 * 200 * @return 201 * The number of rows affected by the update. 202 * 203 * @throws DBException 204 * If execution of the update failed. 205 */ 206 public static int executeUpdate(Provider provider, String sql, List parms) 207 throws SQLException, DBException { 208 logger.info("METHOD_ENTRY executeUpdate"); 209 210 int rowsAffected = 0; 211 212 Connection conn = null; 213 214 try { 215 // get connection, create statement and execute 216 conn = ConnectionFactory.getConnection(provider); 217 rowsAffected = executeUpdate(conn, sql, parms); 218 } catch (DBException dbE) { 219 logger.error(dbE.getMessage()); 220 throw dbE; 221 } catch (Exception e) { 222 logger.error(e.getMessage()); 223 throw new DBException(e.getMessage()); 224 } finally { 225 try { 226 conn.close(); 227 conn = null; 228 } catch (Exception e) { 229 ; 230 } 231 } 232 233 logger.info("METHOD_EXIT executeUpdate"); 234 235 return rowsAffected; 236 } 237 238 /*** 239 * Execute the supplied SQL query against a database connection 240 * supplied from <code>ConnectionFactory</code>. 241 * 242 * @param sql 243 * The SQL query to execute 244 * 245 * @param returnType 246 * int defining the return object types, enumerated on DatabaseProxy 247 * 248 * @return Object 249 * The SQL results as the object type requested 250 * 251 * @throws DBException 252 * If execution of the query failed. 253 * @deprecated Use one of the other methods that does not referende 254 * return type 255 */ 256 public static Object executeQuery(String sql, int returnType) 257 throws DBException { 258 logger.info("METHOD_ENTRY: sql: " + sql); 259 260 Object result = null; 261 262 try { 263 // get connection, create statement and execute 264 Connection conn = ConnectionFactory.getConnection(); 265 266 result = executeQuery(conn, sql, returnType); 267 conn.close(); 268 } catch (DBException dbe) { 269 // already logged, just rethrow 270 throw dbe; 271 } catch (Exception e) { 272 logger.error(e.getMessage()); 273 throw new DBException("ERROR: " + e.getMessage()); 274 } finally { 275 } 276 277 logger.info("METHOD_EXIT"); 278 279 return result; 280 } 281 282 /*** 283 * Execute the supplied SQL query against a database connection 284 * supplied from <code>ConnectionFactory</code>. 285 * 286 * @param sql 287 * The SQL query to execute 288 * 289 * @return List 290 * The SQL results where each element of the list represents a row. 291 * Each row is held in a Map of column name = column value pairs. 292 * 293 * @throws DBException 294 * If execution of the query failed. 295 */ 296 public static List executeQuery(Provider prov, String sql) 297 throws DBException { 298 logger.info("METHOD_ENTRY: sql: " + sql); 299 300 List result = null; 301 302 Connection conn = null; 303 304 try { 305 // get connection, create statement and execute 306 conn = ConnectionFactory.getConnection(prov); 307 result = executeQuery(conn, sql); 308 } catch (DBException dbe) { 309 // already logged, just rethrow 310 throw dbe; 311 } catch (Exception e) { 312 logger.error(e.getMessage()); 313 throw new DBException("ERROR: " + e.getMessage()); 314 } finally { 315 try { 316 conn.close(); 317 } catch (SQLException e) { 318 ; 319 } 320 321 conn = null; 322 } 323 324 logger.info("METHOD_EXIT"); 325 326 return result; 327 } 328 329 /*** 330 * Execute the supplied SQL query against a database connection 331 * supplied from <code>ConnectionFactory</code>. 332 * 333 * @param sql 334 * The SQL query to execute 335 * 336 * @return List 337 * The SQL results where each element of the list represents a row. 338 * Each row is held in a Map of column name = column value pairs. 339 * 340 * @throws DBException 341 * If execution of the query failed. 342 */ 343 public static List executeQuery(Provider prov, String sql, List parms) 344 throws DBException { 345 logger.info("METHOD_ENTRY: sql: " + sql + ", parms: " + parms); 346 347 List result = null; 348 349 Connection conn = null; 350 351 try { 352 // get connection, create statement and execute 353 conn = ConnectionFactory.getConnection(prov); 354 result = executeQuery(conn, sql, parms); 355 } catch (DBException dbe) { 356 // already logged, just rethrow 357 throw dbe; 358 } catch (Exception e) { 359 logger.error(e.getMessage()); 360 throw new DBException("ERROR: " + e.getMessage()); 361 } finally { 362 try { 363 conn.close(); 364 } catch (SQLException e) { 365 ; 366 } 367 368 conn = null; 369 } 370 371 logger.info("METHOD_EXIT"); 372 373 return result; 374 } 375 376 /*** 377 * Execute the supplied stored procedure against a database connection 378 * supplied from <code>ConnectionFactory</code>. 379 * 380 * @param sp 381 * A string containing the stored procedure name 382 * 383 * @param parms 384 * <code>ArrayList</code> holding parameters required for stored proc 385 * 386 * @return int 387 * The number of rows affected by the stored procedure executed 388 * 389 * @throws DBException 390 * If execution of the query failed 391 */ 392 public static int executeDmlProcedure(String sp, ArrayList parms) 393 throws DBException { 394 logger.info("METHOD_ENTRY: sp, parms: " + sp + "," + parms.toString()); 395 396 int i = 0; 397 398 try { 399 // get connection, create statement and execute 400 Connection conn = ConnectionFactory.getConnection(); 401 402 executeDmlProcedure(conn, sp, parms); 403 conn.close(); 404 } catch (DBException dbE) { 405 logger.error(dbE.getMessage()); 406 throw dbE; 407 } catch (Exception e) { 408 logger.error(e.getMessage()); 409 throw new DBException(e.getMessage()); 410 } finally { 411 } 412 413 logger.info("METHOD_EXIT"); 414 415 return i; 416 } 417 418 /*** 419 * Execute the supplied stored procedure against a database connection 420 * supplied from <code>ConnectionFactory</code>. 421 * 422 * @param sp 423 * A string containing the stored procedure name 424 * 425 * @param parms 426 * <code>ArrayList</code> holding parameters required for stored proc 427 * 428 * @param returnType 429 * int defining the return object types, enumerated on DatabaseProxy 430 * 431 * @return object 432 * <code>java.lang.Object</code> containing any rows found 433 * Object type matches the enumeration in <code>DatabaseProxy</code> 434 * 435 * @throws DBException 436 * If execution of the update failed. 437 */ 438 public static java.lang.Object executeQueryProcedure(String sp, List parms, 439 int returnType) 440 throws DBException { 441 logger.info("METHOD_ENTRY sp, parms: " + sp + "," + parms.toString()); 442 443 Object o = null; 444 445 try { 446 // get connection, create statement and execute 447 Connection conn = ConnectionFactory.getConnection(); 448 449 o = executeQueryProcedure(conn, sp, parms, returnType); 450 } catch (DBException dbE) { 451 logger.error(dbE.getMessage()); 452 throw dbE; 453 } catch (Exception e) { 454 logger.error(e.getMessage()); 455 throw new DBException(e.getMessage()); 456 } finally { 457 } 458 459 logger.info("METHOD_EXIT"); 460 461 return o; 462 } 463 }

This page was automatically generated by Maven