package com.firefly.db;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import com.firefly.utils.log.Log;
import com.firefly.utils.log.LogFactory;

public class JDBCHelper {

	private final static Log log = LogFactory.getInstance().getLog("firefly-system");

	private DataSource dataSource;
	private final QueryRunner runner = new QueryRunner();

	public JDBCHelper() {
	}

	public JDBCHelper(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public DataSource getDataSource() {
		return dataSource;
	}

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public <T> T querySingleColumn(String sql, Object... params) {
		try (Connection connection = dataSource.getConnection()) {
			return runner.query(sql, new ScalarHandler<T>(), params);
		} catch (SQLException e) {
			log.error("query exception, sql: {}", e, sql);
			return null;
		}
	}

	public <T> T query(String sql, Class<T> t, Object... params) {
		try (Connection connection = dataSource.getConnection()) {
			return runner.query(sql, new BeanHandler<T>(t), params);
		} catch (SQLException e) {
			log.error("query exception, sql: {}", e, sql);
			return null;
		}
	}

	public <T> List<T> queryForList(String sql, Class<T> t, Object... params) {
		try (Connection connection = dataSource.getConnection()) {
			return runner.query(sql, new BeanListHandler<T>(t), params);
		} catch (SQLException e) {
			log.error("query exception, sql: {}", e, sql);
			return null;
		}
	}

}

@@ -22,36 +22,8 @@
 b;%0A%0A
-import java.sql.Connection;%0A
 impo
@@ -102,16 +102,175 @@
 ource;%0A%0A
+import org.apache.commons.dbutils.BasicRowProcessor;%0Aimport org.apache.commons.dbutils.BeanProcessor;%0Aimport org.apache.commons.dbutils.GenerousBeanProcessor;%0A
 import o
@@ -679,16 +679,22 @@
 private 
+final 
 DataSour
@@ -745,56 +745,84 @@
 nner
- = new QueryRunner();%0A%0A%09public JDBCHelper() %7B%0A%09%7D
+;%0A%09private BeanProcessor defaultBeanProcessor = new GenerousBeanProcessor();
 %0A%0A%09p
@@ -887,32 +887,77 @@
 e = dataSource;%0A
+%09%09this.runner = new QueryRunner(dataSource);%0A
 %09%7D%0A%0A%09public Data
@@ -1018,82 +1018,265 @@
 lic 
-void setDataSource(DataSource dataSource) %7B%0A%09%09this.dataSource = dataSource
+QueryRunner getRunner() %7B%0A%09%09return runner;%0A%09%7D%0A%0A%09public BeanProcessor getDefaultBeanProcessor() %7B%0A%09%09return defaultBeanProcessor;%0A%09%7D%0A%0A%09public void setDefaultBeanProcessor(BeanProcessor defaultBeanProcessor) %7B%0A%09%09this.defaultBeanProcessor = defaultBeanProcessor
 ;%0A%09%7D
@@ -1350,61 +1350,8 @@
 %09try
- (Connection connection = dataSource.getConnection())
  %7B%0A%09
@@ -1583,64 +1583,167 @@
 %7B%0A%09%09
-try (Connection connection = dataSource.getConnection())
+return this.query(sql, t, defaultBeanProcessor, params);%0A%09%7D%0A%0A%09public %3CT%3E T query(String sql, Class%3CT%3E t, BeanProcessor beanProcessor, Object... params) %7B%0A%09%09try
  %7B%0A%09
@@ -1781,32 +1781,70 @@
 BeanHandler%3CT%3E(t
+, new BasicRowProcessor(beanProcessor)
 ), params);%0A%09%09%7D 
@@ -2025,64 +2025,187 @@
 %7B%0A%09%09
-try (Connection connection = dataSource.getConnection())
+return this.queryForList(sql, t, defaultBeanProcessor, params);%0A%09%7D%0A%0A%09public %3CT%3E List%3CT%3E queryForList(String sql, Class%3CT%3E t, BeanProcessor beanProcessor, Object... params) %7B%0A%09%09try
  %7B%0A%09
@@ -2255,16 +2255,54 @@
 ler%3CT%3E(t
+, new BasicRowProcessor(beanProcessor)
 ), param

add jdbc helper