纯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.mzph.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…