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

}

}

}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392317.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392317.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392317.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數據結構與算法深入學習_我最喜歡的免費課程,用于深入學習數據結構和算法...

數據結構與算法深入學習by javinpaul由javinpaul Data structures and algorithms are some of the most essential topics for programmers, both to get a job and to do well on a job. Good knowledge of data structures and algorithms is the foundation of writing go…

RabbitMQ學習系列(一): 介紹

1、介紹 RabbitMQ是一個由erlang開發的基于AMQP(Advanced Message Queue )協議的開源實現。用于在分布式系統中存儲轉發消息,在易用性、擴展性、高可用性等方面都非常的優秀。是當前最主流的消息中間件之一。 RabbitMQ的官網:http…

詳盡kmp_詳盡的分步指南,用于數據準備

詳盡kmp表中的內容 (Table of Content) Introduction 介紹 What is Data Preparation 什么是數據準備 Exploratory Data Analysis (EDA) 探索性數據分析(EDA) Data Preprocessing 數據預處理 Data Splitting 數據分割 介紹 (Introduction) Before we get into this, I want to …

leetcode 947. 移除最多的同行或同列石頭(dfs)

n 塊石頭放置在二維平面中的一些整數坐標點上。每個坐標點上最多只能有一塊石頭。 如果一塊石頭的 同行或者同列 上有其他石頭存在,那么就可以移除這塊石頭。 給你一個長度為 n 的數組 stones ,其中 stones[i] [xi, yi] 表示第 i 塊石頭的位置&#x…

matlab距離保護程序,基于MATLAB的距離保護仿真.doc

基于MATLAB的距離保護仿真摘要:本文闡述了如何利用Matlab中的Simulink及SPS工具箱建立線路的距離保護仿真模型,并用S函數編制相間距離保護和接地距離保護算法程序,構建相應的保護模塊,實現了三段式距離保護。仿真結果表明&#xf…

ZOJ3385 - Hanami Party (貪心)

題目鏈接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode3385 題目大意: 妖夢要準備一個party,所以需要許多食物,初始化妖夢的烹飪技能為L,每天妖夢有兩種選擇,一是選擇當天做L個食物&am…

sklearn.fit_兩個小時后仍在運行嗎? 如何控制您的sklearn.fit。

sklearn.fitby Nathan Toubiana內森圖比亞納(Nathan Toubiana) 兩個小時后仍在運行嗎? 如何控制您的sklearn.fit (Two hours later and still running? How to keep your sklearn.fit under control) Written by Gabriel Lerner and Nathan Toubiana加布里埃爾勒納…

RabbitMQ學習系列(二): RabbitMQ安裝與配置

1.安裝 Rabbit MQ 是建立在強大的Erlang OTP平臺上,因此安裝RabbitMQ之前要先安裝Erlang。 erlang:http://www.erlang.org/download.html rabbitmq:http://www.rabbitmq.com/download.html 注意: 1.現在先別裝最新的 3…

帝國CMS淺淺滴談一下——博客園老牛大講堂

封筆多月之后,工作中遇到了很多很多的問題,也解決了一些問題,下面我把一些得出的經驗,分享給大家! 會帝國cms的請離開,這篇文章對你沒什么用 1、什么是帝國CMS?---博客園老牛大講堂 多月之前&am…

matlab cdf,Matlab 簡單計算PDF和CDF | 學步園

通信的魅力就是在于隨機性中蘊含的確定性,這也就是為什么你隨便拿出一本通信方面的教材,前面幾章都會大篇幅的講解隨機過程,隨機過程也是研究生必須深入了解的一門課,特別是對于信號處理以及通信專業的學生。在實際工作中&#xf…

leetcode 1232. 綴點成線

在一個 XY 坐標系中有一些點,我們用數組 coordinates 來分別記錄它們的坐標,其中 coordinates[i] [x, y] 表示橫坐標為 x、縱坐標為 y 的點。 請你來判斷,這些點是否在該坐標系中屬于同一條直線上,是則返回 true,否則…

mysql常用操作(一)

【數據庫設計的三大范式】1、第一范式(1NF):數據表中的每一列,必須是不可拆分的最小單元。也就是確保每一列的原子性。 例如:userInfo:山東省煙臺市 18865518189 應拆分成 userAds山東省煙臺市 userTel188655181892、第…

pmp 成本估算準確高_如何更準確地估算JavaScript中文章的閱讀時間

pmp 成本估算準確高by Pritish Vaidya通過Pritish Vaidya 準確估算JavaScript中篇文章的閱讀時間 (Accurate estimation of read time for Medium articles in JavaScript) 介紹 (Introduction) Read Time Estimate is the estimation of the time taken by the reader to rea…

Android數據適配-ExpandableListView

Android中ListView的用法基本上學的時候都會使用,其中可以使用ArrayAdapter,SimpleAdapter,BaseAdapter去實現,這次主要使用的ExpandableListView展示一種兩層的效果,ExpandableListView是android中可以實現下拉list的…

JavaWeb 命名規則

命名規范命名規范命名規范命名規范 本規范主要針對java開發制定的規范項目命名項目命名項目命名項目命名 項目創建,名稱所有字母均小寫,組合方式為:com.company.projectName.component.hiberarchy。1. projectName:項目名稱2. com…

多元概率密度_利用多元論把握事件概率

多元概率密度Humans have plenty of cognitive strengths, but one area that most of us struggle with is estimating, explaining and preparing for improbable events. This theme underpins two of Nassim Taleb’s major works: Fooled by Randomness and The Black Swa…

nginx php訪問日志配置,nginx php-fpm 輸出php錯誤日志的配置方法

由于nginx僅是一個web服務器,因此nginx的access日志只有對訪問頁面的記錄,不會有php 的 error log信息。nginx把對php的請求發給php-fpm fastcgi進程來處理,默認的php-fpm只會輸出php-fpm的錯誤信息,在php-fpm的errors log里也看不…

阿里的技術愿景_技術技能的另一面:領域知識和長期愿景

阿里的技術愿景by Sihui Huang黃思慧 技術技能的另一面:領域知識和長期愿景 (The other side of technical skill: domain knowledge and long-term vision) When we first start our careers as software engineers, we tend to focus on improving our coding sk…

leetcode 721. 賬戶合并(并查集)

給定一個列表 accounts,每個元素 accounts[i] 是一個字符串列表,其中第一個元素 accounts[i][0] 是 名稱 (name),其余元素是 emails 表示該賬戶的郵箱地址。 現在,我們想合并這些賬戶。如果兩個賬戶都有一些共同的郵箱地址&#…

es6重點筆記:數值,函數和數組

本篇全是重點,撿常用的懟,數值的擴展比較少,所以和函數放一起: 一,數值 1,Number.EPSILON:用來檢測浮點數的計算,如果誤差小于這個,就無誤 2,Math.trunc()&am…