当前位置: 首页 > 工具软件 > BoneCP > 使用案例 >

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

席俊达
2023-12-01

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

}

}

}

 类似资料: