mysql的连接池配置文件,数据库连接池/配置文件数据库连接池

1.方式

一种是使用直接c3p0数据库连接池,一种是使用配置文件将连接信息写在配置文件中。

将连接信息写在配置文件中有很多好处,比如如果想替换一个线上项目的数据库,只要改变配置文件中的链接信息就可以了。再就是一些需要修改的地方,比如想要修改链接池的属性,也只需要修配置文件就可以了,不需要去修改java代码,那样还需要重新编译生成class文件,重新运行一遍。

aceb18ea790a

2.直接使用jar包

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

package com.begin21.w1106.ClassTest.excute;

import java.beans.PropertyVetoException;

import java.sql.Connection;

import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

//数据库连接池

public class ConnectionPool {

private static ComboPooledDataSource comboPooledDataSource;

//初始化连接池

public static final void initDBSource(){

comboPooledDataSource = new ComboPooledDataSource();

try {

comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");

} catch (PropertyVetoException e) {

e.printStackTrace();

}

comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/gaobo");

comboPooledDataSource.setUser("root");

comboPooledDataSource.setPassword("");

comboPooledDataSource.setMinPoolSize(3); //最小线程数

comboPooledDataSource.setMaxPoolSize(20);//最大线程数

comboPooledDataSource.setInitialPoolSize(5);//初始线程数

comboPooledDataSource.setAcquireIncrement(2);//每次新增线程数

comboPooledDataSource.setMaxIdleTime(5);//线程空闲时间

}

/**

* 从连接池中获取连接

* @return

*/

public static synchronized Connection getConnection() {

Connection conn = null;

try {

conn = comboPooledDataSource.getConnection();

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

public static void closeOpenResource(AutoCloseable resource) {

if (null != resource) {

try {

resource.close();

} catch (Exception e) {

e.printStackTrace();

}}}}

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

package com.begin21.w1106.ClassTest.excute;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

public class Main2 {

public static void main(String[] args) {

ConnectionPool.initDBSource();

query();}

private static void query() {

Connection conn = ConnectionPool.getConnection();

PreparedStatement prst = null;

ResultSet rs = null;

String sql = "select * from repo_sto";

try {

prst = conn.prepareStatement(sql);

rs = prst.executeQuery();

while(rs.next()) {

System.out.println(rs.getString(2));}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

ConnectionPool.closeOpenResource(rs);

ConnectionPool.closeOpenResource(prst);

// close不是关闭数据库连接池的连接对象,而是把它放回连接池

ConnectionPool.closeOpenResource(conn);}}}

3.使用配置文件

配置文件 db.properties

package com.begin21.w1106.ClassTest.excute;

import java.beans.PropertyVetoException;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.SQLException;

import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class ConnectionPoolProp {

private static ComboPooledDataSource comboPooledDataSource;

public static final void initDBSource() {

Properties properties = new Properties();

try {

File file = new File(ConnectionPoolProp.class.getResource("/").getFile().toString() + "dbs.properties");  //引用配置文件

System.out.println(ConnectionPoolProp.class.getResource("/").getFile());

InputStream inputStream = new FileInputStream(file);

properties.load(inputStream);

} catch (IOException e) {

e.printStackTrace();

}

comboPooledDataSource = new ComboPooledDataSource();

try {

comboPooledDataSource.setDriverClass(properties.getProperty("driver"));

} catch (PropertyVetoException e) {

e.printStackTrace();

}

comboPooledDataSource.setJdbcUrl(properties.getProperty("url"));

comboPooledDataSource.setUser(properties.getProperty("user"));

comboPooledDataSource.setPassword(properties.getProperty("password"));

comboPooledDataSource.setMinPoolSize(Integer.parseInt(properties.getProperty("c3p0.minPoolSize")));

comboPooledDataSource.setMaxPoolSize(Integer.parseInt(properties.getProperty("c3p0.maxPoolSize")));

comboPooledDataSource.setInitialPoolSize(Integer.parseInt(properties.getProperty("c3p0.initPoolSize")));

comboPooledDataSource.setAcquireIncrement(Integer.parseInt(properties.getProperty("c3p0.acquireIncrement")));

comboPooledDataSource.setMaxStatements(Integer.parseInt(properties.getProperty("c3p0.maxStatements").trim()));

comboPooledDataSource.setMaxIdleTime(Integer.parseInt(properties.getProperty("c3p0.maxIdleTime")));

}

public static synchronized Connection getConnection() {

Connection conn = null;

try {

conn = comboPooledDataSource.getConnection();

} catch (SQLException e) {

e.printStackTrace();

}

return conn;

}

public static void closeOpenResource(AutoCloseable resource) {

if (null != resource) {

try {

resource.close();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();}}}}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值