package com.yiidata.amc.jdbc import java.util import java.util.Properties import org.apache.commons.dbcp.BasicDataSource import org.apache.commons.dbutils.QueryRunner import org.apache.commons.dbutils.handlers.MapListHandler /** *

* 从数据库中读取数据 *

* * Created by ZhenQin on 2018/2/10 0010-10:35 * Vendor: yiidata.com * @author ZhenQin */ trait JdbcReader[T] { def read(sql:String, ps:Properties):T } /** * JDBC 读取表数据 * * @author ZhenQin * */ class JdbcRowReader extends JdbcReader[util.List[util.Map[String, AnyRef]]] { override def read(sql:String, ps: Properties): util.List[util.Map[String, AnyRef]] = { val dataSource = new BasicDataSource() dataSource.setMaxActive(1) dataSource.setMinIdle(1) dataSource.setInitialSize(1) dataSource.setDriverClassName(ps.getProperty("driver")) dataSource.setUrl(ps.getProperty("url")) dataSource.setUsername(ps.getProperty("user")) dataSource.setPassword(ps.getProperty("password")) val qr = new QueryRunner(dataSource) try { val list: util.List[util.Map[String, AnyRef]] = qr.query(sql, new MapListHandler()) list } finally { dataSource.close() } } }