Java 获取各种主流数据库连接池连接信息的方法

package com.gb.core.util;

import javax.sql.DataSource;

import org.apache.log4j.Logger;
import org.enhydra.jdbc.pool.StandardPoolDataSource;
import org.enhydra.jdbc.standard.StandardConnectionPoolDataSource;
import org.logicalcobwebs.proxool.ProxoolDataSource;
import org.springframework.jdbc.core.JdbcTemplate;

import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * <p>Title: </p>
 * <p>desc:
 * <p>Copyright: Copyright(c)Gb 2011</p>
 * @author www.bejson.com
 * @time 2011-8-23 上午10:37:35
 * @version 1.0
 * @since
 */
public class DBUtil {
	public static  String showDBInfo(JdbcTemplate jdbcTemplate){
		try {
			DataSource ds =   jdbcTemplate.getDataSource();
			String driver = "";
			String connectInfo="";
			String user="";
			String password="";
			if(ds instanceof StandardPoolDataSource){
			StandardPoolDataSource sd = (StandardPoolDataSource)ds;
			StandardConnectionPoolDataSource scd = (StandardConnectionPoolDataSource)sd.cpds;
				driver = scd.getDriverName();
				connectInfo =  scd.getUrl();
				user = sd.getUser();
				password = sd.getPassword();
			}else if(ds instanceof DruidDataSource){
				DruidDataSource dds = (DruidDataSource)jdbcTemplate.getDataSource();
				driver = dds.getDriverClassName();
				connectInfo =  dds.getUrl();
				user = dds.getUsername();
				password = dds.getPassword();
			} else if(ds instanceof ProxoolDataSource){
				ProxoolDataSource  db = (ProxoolDataSource)jdbcTemplate.getDataSource();
				 password = 	db.getPassword();
				 user = db.getUser();
				 connectInfo = db.getDriverUrl();
			} else if(ds instanceof ComboPooledDataSource){
				ComboPooledDataSource  db = (ComboPooledDataSource)jdbcTemplate.getDataSource();
				driver = 	db.getDriverClass().toLowerCase();
				connectInfo = db.getJdbcUrl();
				user = db.getUser();
				password =db.getPassword();
			}
		
			return " url:"+connectInfo+" user:"+user+" password:"+password;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return "";
	}
	
	private static Logger log = Logger.getLogger(DBUtil.class);
}



转载于:https://my.oschina.net/ij2ee/blog/325773

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值