View Javadoc

1   /*
2    * The contents of this file are subject to the terms 
3    * of the Common Development and Distribution License 
4    * (the "License").  You may not use this file except 
5    * in compliance with the License.
6    * 
7    * You can obtain a copy of the license at 
8    * http://www.sun.com/cddl/cddl.html. 
9    * See the License for the specific language governing 
10   * permissions and limitations under the License.
11   * 
12   * When distributing Covered Code, include this CDDL 
13   * HEADER in each file and include the License file at 
14   * license.txt.  If applicable, add the following below 
15   * this CDDL HEADER, with the fields enclosed by brackets 
16   * "[]" replaced with your own identifying information: 
17   * Portions Copyright [yyyy] [name of copyright owner]
18   * 
19   * Portions Copyright 2004 eBay, Inc.
20   * Portions Copyright 2009 Jeremy Thomerson
21   */
22  package com.ebay.carad.os.vitalsigns.dao;
23  
24  import java.sql.SQLException;
25  import java.util.List;
26  
27  import javax.sql.DataSource;
28  
29  import org.apache.commons.dbutils.QueryRunner;
30  import org.apache.commons.dbutils.handlers.MapListHandler;
31  import org.apache.log4j.Logger;
32  
33  import com.ebay.carad.os.vitalsigns.ReportingException;
34  
35  /***
36   * Straight-forward implementation of ISqlDAO that utilizes commons-dbutils.
37   *  
38   * @author Jeremy Thomerson
39   * @version $Id$
40   */
41  public class SqlDAO implements ISqlDAO {
42  
43      private static final Logger LOGGER = Logger.getLogger(SqlDAO.class);
44      private QueryRunner mQueryRunner = new QueryRunner();
45      private MapListHandler mMapListHandler = new MapListHandler();
46      
47      public List findBySqlQuery(String query) {
48          try {
49              return (List) mQueryRunner.query(query, mMapListHandler);
50          } catch (SQLException ex) {
51              String message = "Exception while executing query [" + query + "]: " + ex.getMessage();
52              LOGGER.error(message, ex);
53              throw new ReportingException(message, ex);
54          }
55      }
56  
57      public int updateWithSQL(String query) {
58          try {
59              return mQueryRunner.update(query);
60          } catch (SQLException ex) {
61              String message = "Exception while executing query [" + query + "]: " + ex.getMessage();
62              LOGGER.error(message, ex);
63              throw new ReportingException(message, ex);
64          }
65      }
66  
67      public void setDataSource(DataSource dataSource) {
68      	mQueryRunner = new QueryRunner(dataSource);
69      }
70  }
71