1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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