java获取连接池_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;

/**

*

Title:

*

desc:

*

Copyright: Copyright(c)Gb 2011

* @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);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用Druid连接池创建Java工具类的步骤: 1. 首先,需要将Druid的jar包导入到项目中。可以从Maven仓库或者Druid官网下载。 2. 在Java工具类中导入Druid需要的包: ```java import com.alibaba.druid.pool.DruidDataSource; import java.sql.Connection; import java.sql.SQLException; ``` 3. 创建Druid连接池,可以在工具类的静态代码块中完成: ```java public class DatabaseUtils { private static DruidDataSource dataSource; static { dataSource = new DruidDataSource(); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8"); dataSource.setUsername("root"); dataSource.setPassword("123456"); dataSource.setInitialSize(5); // 初始化连接dataSource.setMaxActive(10); // 最大连接dataSource.setMinIdle(2); // 最小空闲连接dataSource.setMaxWait(60000); // 获取连接的最大等待时间,单位毫秒 } } ``` 4. 编写获取数据库连接的方法: ```java public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } ``` 5. 在需要使用数据库连接的地方,调用上面编写的getConnection方法即可获取连接。 ```java public static void main(String[] args) { Connection conn = null; try { conn = DatabaseUtils.getConnection(); // do something with the connection } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ``` 以上就是使用Druid连接池创建Java工具类的全部步骤。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值