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);
}



你可能感兴趣的:(java,数据库,连接池)