1 package com.imcode.db; 2 3 import org.apache.commons.dbutils.QueryRunner; 4 import org.apache.commons.dbutils.ResultSetHandler; 5 6 import java.sql.Connection; 7 import java.sql.SQLException; 8 9 public class JdbcDatabaseConnection implements DatabaseConnection { 10 11 private Connection connection; 12 private QueryRunner queryRunner = new QueryRunner(); 13 14 public JdbcDatabaseConnection(Connection connection) { 15 this.connection = connection; 16 } 17 18 public int executeUpdate(String sql, Object[] parameters) throws DatabaseException { 19 try { 20 return queryRunner.update(getConnection(), sql, parameters); 21 } catch ( SQLException se) { 22 throw DatabaseException.fromSQLException(se); 23 } 24 } 25 26 public Number executeUpdateAndGetGeneratedKey(String sql, Object[] parameters) throws DatabaseException { 27 try { 28 return JdbcUtils.executeUpdateAndGetGeneratedKey(getConnection(), sql, parameters); 29 } catch (SQLException se) { 30 throw DatabaseException.fromSQLException(se); 31 } 32 } 33 34 public Object executeQuery(String sqlQuery, Object[] parameters, 35 ResultSetHandler resultSetHandler) throws DatabaseException { 36 try { 37 return queryRunner.query(getConnection(), sqlQuery, parameters, resultSetHandler); 38 } catch (SQLException e) { 39 throw DatabaseException.fromSQLException(e); 40 } 41 } 42 43 public Connection getConnection() { 44 return connection; 45 } 46 }