纯java应用搭建,16、BoneCp纯java项目使用

2、代码实现 package com.study;

import com.jolbox.bonecp.BoneCP;

import com.jolbox.bonecp.BoneCPConfig;

import com.jolbox.bonecp.BoneCPDataSource;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.sql.*;

/**

* Boncp 纯java处理

* @CreateTime 2018/3/14 14:31

*/

public class BonecpJdbcManager {

private static final Logger LOGGER = LoggerFactory.getLogger(BonecpJdbcManager.class);

private static BonecpJdbcManager instance;

//第一种方式

private BoneCP connectionPool;

//第二种方式

private BoneCPDataSource dataSourcePool;

private BonecpJdbcManager() {

}

private BonecpJdbcManager(String driverName, String jdbcUrl, String userName, String passwd) {

try {

Class.forName(driverName);

//设置连接池配置信息

BoneCPConfig config = new BoneCPConfig();

config.setJdbcUrl(jdbcUrl);

config.setUsername(userName);

config.setPassword(passwd);

//数据库连接池的最小连接数

config.setMinConnectionsPerPartition(5);

//数据库连接池的最大连接数

config.setMaxConnectionsPerPartition(10);

config.setPartitionCount(1);

//根据连接池配置,创建数据库连接池

connectionPool = new BoneCP(config);

// dataSourcePool = new BoneCPDataSource(config);

} catch (ClassNotFoundException e) {

LOGGER.error("ClassNotFoundException for driver : {}", driverName);

} catch (SQLException e) {

LOGGER.error("error to new BoneCp(), exception:{}", e);

}

}

/**

* 单例设计模式

*

* [@param](https://my.oschina.net/u/2303379) driverName

* [@param](https://my.oschina.net/u/2303379) jdbcUrl

* [@param](https://my.oschina.net/u/2303379) userName

* [@param](https://my.oschina.net/u/2303379) passwd

* @return

*/

public static BonecpJdbcManager getInstance(String driverName, String jdbcUrl, String userName, String passwd) {

if (instance == null) {

synchronized ("buildInstance") {

if (instance == null) {

instance = new BonecpJdbcManager(driverName, jdbcUrl, userName, passwd);

}

}

}

return instance;

}

/**

* 获取一个连接

*

* @return

*/

public synchronized Connection getConnection() {

try {

return connectionPool.getConnection();

// return dataSourcePool.getConnection();

} catch (SQLException e) {

LOGGER.error("从连接池中获取连接失败,ERROR:{}", e);

}

return null;

}

/**

* 关闭连接

*

* @param connection

* @return

*/

public synchronized boolean returnConnection(Connection connection) {

try {

connection.close();

return true;

} catch (SQLException e) {

LOGGER.error("回收连接到连接池失败,ERROR:{}", e);

}

return false;

}

/**

* 关闭statement和resultset

*

* @return

*/

public boolean closeStatment(Statement statement, ResultSet resultSet) {

try {

resultSet.close();

statement.close();

return true;

} catch (SQLException e) {

LOGGER.error("关闭statement和resultset失败,ERROR:{}", e);

}

return false;

}

/**

* 测试

* @param args

*/

public static void main(String[] args) {

String driverName = "com.mysql.jdbc.Driver";

String jdbcUrl = "jdbc:mysql://localhost:3306/caiwutong";

String userName = "root";

String passwd = "123456";

BonecpJdbcManager instance = BonecpJdbcManager.getInstance(driverName, jdbcUrl, userName, passwd);

Connection connection = instance.getConnection();

PreparedStatement statement = null;

ResultSet resultSet = null;

String sql = "select * from user_info";

try {

statement = connection.prepareStatement(sql);

resultSet = statement.executeQuery();

while (resultSet.next()) {

System.out.println(resultSet.getString("id"));

}

} catch (SQLException e) {

e.printStackTrace();

} finally {

instance.closeStatment(statement, resultSet);

instance.returnConnection(connection);

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值