sanshao的gravatar头像
sanshao 2014-10-10 16:45:24

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

打赏

顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友