首页 技术 正文
技术 2022年11月17日
0 收藏 334 点赞 2,299 浏览 3714 个字

环境InterlliJ2016.3  MySQL5.7.12

pom依赖:

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.7.2</version>
</dependency>

Hikari java数据库连接池实战

配置文件db.properties

db_url = 192.168.199.132
db_port = 3306
db_name = mind
db_max_conn = 100
db_username = root
db_password = root

Hikari java数据库连接池实战

DBService.Java:

package com.mind.core.db.impl;import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;/**
* 数据库服务
* Created by Lovell on 16/6/18.
*/
public class DBService {
private static Logger logger = LoggerFactory.getLogger(DBService.class); private static final String DB_CONFIG_FILE = "/db.properties"; // 数据库连接数
private short db_max_conn = 0; // 数据库服务器addr
private String db_url = null; // 数据库连接端口
private short db_port = 0; // 数据库名称
private String db_name = null; // 数据库登录用户名
private String db_username = null; // 数据库登录密码
private String db_password = null; // 数据库连接
private Connection connection; private static DBService dBService;
public static DBService getInstance(){
if (dBService == null) {
dBService = new DBService();
}
return dBService;
} public void start() throws IOException, SQLException {
Properties properties = new Properties();
InputStream in = DBService.class.getClass().getResourceAsStream(DB_CONFIG_FILE);
properties.load(in); db_max_conn = Short.valueOf(properties.getProperty("db_max_conn"));
db_url = String.valueOf(properties.getProperty("db_url"));
db_port = Short.valueOf(properties.getProperty("db_port"));
db_name = String.valueOf(properties.getProperty("db_name"));
db_username = String.valueOf(properties.getProperty("db_username"));
db_password = String.valueOf(properties.getProperty("db_password")); if (db_url == null || db_url.length() == 0) {
logger.error("配置的数据库ip地址错误!");
System.exit(0);
} HikariConfig config = new HikariConfig();
config.setMaximumPoolSize(db_max_conn);
config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
config.addDataSourceProperty("serverName", db_url);
config.addDataSourceProperty("port", db_port);
config.addDataSourceProperty("databaseName", db_name);
config.addDataSourceProperty("user", db_username);
config.addDataSourceProperty("password", db_password);
HikariDataSource dataSource = new HikariDataSource(config);// // 也可以这样写
// config.setDriverClassName("com.mysql.jdbc.Driver");
// config.setJdbcUrl("jdbc:mysql://"+ db_url +"/" + db_name + "?useUnicode=true&characterEncoding=utf8&useSSL=false");
// config.setUsername(db_username);
// config.setPassword(db_password);
// config.addDataSourceProperty("cachePrepStmts", "true");
// config.addDataSourceProperty("prepStmtCacheSize", "250");
// config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
// // 设置连接超时为8小时
// config.setConnectionTimeout(8 * 60 * 60);
// HikariDataSource dataSource = new HikariDataSource(config);
} public Connection getConnection() throws SQLException {
try {
return dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
dataSource.resumePool();
return null;
} } public boolean stop() throws SQLException {
dataSource.close();
return true;
}
}

Hikari java数据库连接池实战

DBServiceTest.java

package com.mind.core.db.impl;import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;/**
* Created by Lovell on 16/6/25.
*/
public class DBServiceTest {
public static void main(String[] args) throws IOException, SQLException {
DBSservice.getInstance().start(); // statement用来执行SQL语句
Statement statement = DBService.getInstance().getConnection().createStatement(); // 要执行的SQL语句id和content是表review中的项。
String sql = "select * from login where name='Lovell' and password='123456'"; // 得到结果
ResultSet rs = statement.executeQuery(sql); if(rs.next()){
System.out.println("Logon"); }else{
System.out.println("Login Faild");
}
rs.close();
}
}
 

Hikari java数据库连接池实战

———————

参考资料:https://blog.csdn.net/langzi7758521/article/details/51766754

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,078
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,553
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,402
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,177
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,814
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898