热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

1.方式一种是使用直接c3p0数据库连接池,一种是使用配置文件将连接信息写在配置文件中。将连接信息写在配置文件中有很多好处,比如如果想替换一个线上项目的

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



推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别? ... [详细]
  • 欢乐的票圈重构之旅——RecyclerView的头尾布局增加
    项目重构的Git地址:https:github.comrazerdpFriendCircletreemain-dev项目同步更新的文集:http:www.jianshu.comno ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • 重入锁(ReentrantLock)学习及实现原理
    本文介绍了重入锁(ReentrantLock)的学习及实现原理。在学习synchronized的基础上,重入锁提供了更多的灵活性和功能。文章详细介绍了重入锁的特性、使用方法和实现原理,并提供了类图和测试代码供读者参考。重入锁支持重入和公平与非公平两种实现方式,通过对比和分析,读者可以更好地理解和应用重入锁。 ... [详细]
  • 解决.net项目中未注册“microsoft.ACE.oledb.12.0”提供程序的方法
    在开发.net项目中,通过microsoft.ACE.oledb读取excel文件信息时,报错“未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序”。本文提供了解决这个问题的方法,包括错误描述和代码示例。通过注册提供程序和修改连接字符串,可以成功读取excel文件信息。 ... [详细]
  • 本文介绍了MVP架构模式及其在国庆技术博客中的应用。MVP架构模式是一种演变自MVC架构的新模式,其中View和Model之间的通信通过Presenter进行。相比MVC架构,MVP架构将交互逻辑放在Presenter内部,而View直接从Model中读取数据而不是通过Controller。本文还探讨了MVP架构在国庆技术博客中的具体应用。 ... [详细]
author-avatar
手机用户2502922477
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有