DBCP 与 DBUtils 结合 配置

46 篇文章 0 订阅

 pheonix_jdbc.properties 文件

driverClassName=org.apache.phoenix.jdbc.PhoenixDriver
url=jdbc:phoenix:10.1.20.129,10.1.20.128,10.1.20.44:2181
username=user
password=pass
initialSize=10
maxIdle=20
minIdle=5
maxActive=50
logAbandoned=true
removeAbandoned=true
removeAbandonedTimeout=180
maxWait=1000


PhoenixQueryUtils:

public class PhoenixQueryUtils {

    private static BasicDataSource dataSource = null;
    private static QueryRunner runner;

    private PhoenixQueryUtils() {
    }

    private synchronized static void init() throws SQLException {
        if (dataSource != null) {
            try {
                dataSource.close();
            } catch (SQLException ex) {
                DbUtils.printStackTrace(ex);
            }
        }
        dataSource = null;
        InputStream in = Object.class.getResourceAsStream("/pheonix_jdbc.properties");
        Properties props = new Properties();
        try {
            props.load(in);
            dataSource = BasicDataSourceFactory.createDataSource(props);
            runner = new QueryRunner(dataSource);
        } catch (IOException ex) {
            throw new SQLException(ex);
        } catch (Exception ex) {
            throw new SQLException(ex);
        }

    }

    public static List<Object[]> getAllResults(String sql, Object[] params) throws SQLException {
        if (runner == null) {
            init();
        }
        List<Object[]> ojbs = runner.query(sql, new ArrayListHandler(), params);
        return ojbs;
    }

    public static Object[] getFirstResult(String sql, Object[] params) throws SQLException {
        if (runner == null) {
            init();
        }
        Object[] ojbs = runner.query(sql, new ArrayHandler(), params);
        return ojbs;
    }

    public static Map<String, Object> getFirstResultMap(String sql, Object[] params) throws SQLException {
        if (runner == null) {
            init();
        }
        Map<String, Object> map = runner.query(sql, new MapHandler(), params);
        return map;
    }

    public static Object getResultMaps(String sql, Object[] params) throws SQLException {
        if (runner == null) {
            init();
        }
        List<Map<String, Object>> list = runner.query(sql, new MapListHandler(), params);
        return list;
    }

    public static synchronized Connection getConnection() throws SQLException, IOException, Exception {
        Connection conn = null;
        if (dataSource == null) {
            init();
        }
        if (dataSource != null) {
            conn = dataSource.getConnection();
        }
        return conn;
    }

}


用例:

public static void main(String[] args) {
        
        try {
            List<Object[]> list = PhoenixQueryUtils.getAllResults("select id,   a,  b ,  c ,  d  from test ",
                    new Object[]{});
            System.out.println("id  \t a \t  b \t c \t d");
            for (Object[] objs : list) {
                for (Object obj : objs) {
                    System.out.print(obj + "\t");
                }
                System.out.println();
            }
        } catch (SQLException ex) {
            DbUtils.printStackTrace(ex);
        }
        System.exit(0);
    }

输出:

id  	 a 	  b 	 c 	 d
5555	55	55	55	55555555
1000010	20	30	50	1415248098439
1000011	22	33	55	1415248098440
1000012	24	36	60	1415248098441
1000013	26	39	65	1415248098441
1000014	28	42	70	1415248098442
1000015	30	45	75	1415248098443
1000016	32	48	80	1415248098443
1000017	34	51	85	1415248098444
1000018	36	54	90	1415248098444
1000019	38	57	95	1415248098445
1000020	40	60	100	1415248098446
1000021	42	63	105	1415248098446
1000022	44	66	110	1415248098447
1000023	46	69	115	1415248098447
1000024	48	72	120	1415248098448
1000025	50	75	125	1415248098449
1000026	52	78	130	1415248098449
1000027	54	81	135	1415248098450
1000028	56	84	140	1415248098450




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值