日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2023/12/9 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 纯java应用搭建,16、BoneCp纯java项目使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2、代碼實(shí)現(xiàn) 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);

//設(shè)置連接池配置信息

BoneCPConfig config = new BoneCPConfig();

config.setJdbcUrl(jdbcUrl);

config.setUsername(userName);

config.setPassword(passwd);

//數(shù)據(jù)庫(kù)連接池的最小連接數(shù)

config.setMinConnectionsPerPartition(5);

//數(shù)據(jù)庫(kù)連接池的最大連接數(shù)

config.setMaxConnectionsPerPartition(10);

config.setPartitionCount(1);

//根據(jù)連接池配置,創(chuàng)建數(shù)據(jù)庫(kù)連接池

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

}

}

/**

* 單例設(shè)計(jì)模式

*

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

}

/**

* 獲取一個(gè)連接

*

* @return

*/

public synchronized Connection getConnection() {

try {

return connectionPool.getConnection();

// return dataSourcePool.getConnection();

} catch (SQLException e) {

LOGGER.error("從連接池中獲取連接失敗,ERROR:{}", e);

}

return null;

}

/**

* 關(guān)閉連接

*

* @param connection

* @return

*/

public synchronized boolean returnConnection(Connection connection) {

try {

connection.close();

return true;

} catch (SQLException e) {

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

}

return false;

}

/**

* 關(guān)閉statement和resultset

*

* @return

*/

public boolean closeStatment(Statement statement, ResultSet resultSet) {

try {

resultSet.close();

statement.close();

return true;

} catch (SQLException e) {

LOGGER.error("關(guān)閉statement和resultset失敗,ERROR:{}", e);

}

return false;

}

/**

* 測(cè)試

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

}

}

}

總結(jié)

以上是生活随笔為你收集整理的纯java应用搭建,16、BoneCp纯java项目使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。