配置文件

db.driver=com.mysql.jdbc.Driver
db.url=jdbc\:mysql\://localhost\:3306/mybase
db.user=root
db.pswd=y@ngmin9 #-- 连接池初始化连接数 --
dataSource.initialSize=10
#-- 最大空闲连接 --
dataSource.maxIdle=20
#-- 最小空闲连接 --
dataSource.minIdle=5
#-- 最大连接数 --
dataSource.maxActive=50
#-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 --
dataSource.wait=500

使用普通方式链接数据库

package com.globalroam.util;

import java.io.IOException;
import java.util.Properties; public class DbUtil {
private static Properties properties = new Properties();
public static String driver = null;
public static String url = null;
public static String user = null;
public static String pswd = null; static{
try {
properties.load(DbUtil.class.getClassLoader().getResourceAsStream("resource/db.properties"));
driver = properties.getProperty("db.driver");
url = properties.getProperty("db.url");
user = properties.getProperty("db.user");
pswd = properties.getProperty("db.pswd");
} catch (IOException e) {
e.printStackTrace();
}
}
}

使用连接池获取Connection

package com.globalroam.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties; import org.apache.commons.dbcp.BasicDataSource; public class DBConnectionPool {
private static BasicDataSource dataSource = null;
private static Properties properties = new Properties();
//private static Logger logger = Logger.;
public static void initDataSource() {
try {
properties.load(DBConnectionPool.class.getClass().getResourceAsStream("resource/db.properties"));
dataSource = new BasicDataSource();
dataSource.setDriverClassName(properties.getProperty("db.driver"));
dataSource.setUrl(properties.getProperty("db.url"));
dataSource.setUsername(properties.getProperty("db.user"));
dataSource.setPassword(properties.getProperty("db.pswd")); if(properties.getProperty("dataSource.initialSize") != null) {
dataSource.setInitialSize(Integer.parseInt(properties.getProperty("dataSource.initialSize")));
} if(properties.getProperty("dataSource.maxIdle") != null) {
dataSource.setMaxIdle(Integer.parseInt(properties.getProperty("dataSource.maxIdle")));
} if(properties.getProperty("dataSource.minIdle") != null) {
dataSource.setMinIdle(Integer.parseInt(properties.getProperty("dataSource.minIdle")));
} if(properties.getProperty("dataSource.maxActive") != null && "0".equals(properties.getProperty("dataSource.maxActive"))) {
dataSource.setMaxActive(Integer.parseInt(properties.getProperty("dataSource.maxActive")));
} if(properties.getProperty("dataSource.wait") != null) {
dataSource.setMaxWait(Integer.parseInt(properties.getProperty("dataSource.wait")));
}
} catch (IOException e) {
e.printStackTrace(); }
} public static Connection getConnection() throws SQLException {
Connection conn = null;
if(dataSource == null) {
initDataSource();
}
if(dataSource != null) {
conn = dataSource.getConnection();
}
return conn;
}
}

从配置文件中读取数据获取Connection的更多相关文章

  1. Feign从配置文件中读取url

    Feign的url和name都是可配置的,就是从配置文件中读取的属性值,然后用占位符引用就可以了: ${rpc.url} @FeignClient(name = "me", url ...

  2. spring boot: 从配置文件中读取数据的常用方法(spring boot 2.3.4)

    一,从配置文件中读取数据有哪些方法? 通常有3种用法: 1,直接使用value注解引用得到配置项的值 2,  封装到Component类中再调用 3,  用Environment类从代码中直接访问 生 ...

  3. 【Python学习笔记七】从配置文件中读取参数

    将一些需要更改或者固定的内容存放在配置文件中,通过读取配置文件来获取参数,这样修改以及使用起来比较方便 1.首先是配置文件的写法如下一个environment.ini文件: 里面“[]”存放的是sec ...

  4. spring boot 项目从配置文件中读取maven 的pom.xml 文件标签的内容。

    需求: 将pom.xml 文件中的版本号读取到配置文件并打印到日志中. 第一步: 在pom.xml 中添加以下标签. 第二步: 将version 标签的值读取到配置文件中 这里使用 @@  而不是  ...

  5. 监听tomcat服务器启动/关闭并从配置文件中读取参数进行初始化

    监听tomcat服务器启动/关闭很简单(2步): 1. 建立一个类实现ServletContextListener接口,重写其中的方法(contextDestroyed和contextInitiali ...

  6. C# 从配置文件中读取/写入信息

    读取: var currMemberID = System.Configuration.ConfigurationManager.AppSettings["tolunaMemberID&qu ...

  7. unittest(13)- 从配置文件中读取测试数据

    case.config # 1. http_request.py import requests class HttpRequest: def http_request(self, url, meth ...

  8. 在ASP.NET 5中读取配置文件

    (此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 在ASP.NET 5中摒弃了之前配置文件的基础结构,引入了一个全新配置文件系统.今天推荐的文 ...

  9. java 如何从配置文件(.properties)中读取内容

    1.如何创建.properties文件 很简单,建立一个txt文件,并把后缀改成.properties即可 2.将.properties文件拷入src的根目录下 3..properties文件内容格式 ...

随机推荐

  1. amchart

    amchart能够根据提供的数据便捷的生成好看的图标,曾在项目中遇到使用falsh版以支持对js支持不好的低版本浏览器,但是现在官网上都是js版本的,flash版的文档都没有,搜索结果一般都是链接到博 ...

  2. MySQL主从问题

    Mysql数据库主从心得整理      管理mysql主从有2年多了,管理过200多组mysql主从,几乎涉及到各个版本的主从,本博文属于总结性的,有一部分是摘自网络,大部分是根据自己管理的心得和经验 ...

  3. JQuery 获取自身的HTml代码

    $('.top').append($(this).prop("outerHTML"));

  4. 棋盘上的距离 - Grids1657

    棋盘上的距离 问题描述: 写一个程序,给定起始位置和目标位置,计算王.后.车.象从起始位置走到目标位置所需的最少步数. 王:横.直.斜都可以走,但每步限走一格. 后:横.直.斜都可以走,每步格数不受限 ...

  5. 摘录 javescript 常用函数

     

  6. android开发3:四大基本组件的介绍与生命周期

    android开发3:四大基本组件的介绍与生命周期 Android四大基本组件分别是Activity,Service服务,Content Provider内容提供者,BroadcastReceiver ...

  7. JVM基础和调优(三)

    主要讲解垃圾回收的算法 上面我们已经了解到了,JVM的体系的结构,这次我们来说一下垃圾回收的算法. 1. 最开始的想法,或者说垃圾回收的最容易想到的就是:引用计数(reference count) 我 ...

  8. CodeForces 19D Points

    Pete and Bob invented a new interesting game. Bob takes a sheet of paper and locates a Cartesian coo ...

  9. 使用JS截取字符串函数详解

    一.函数:split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=”jpg|bmp|gif|ico|png”; arr=theString.split(”|”); //a ...

  10. IE常见的CSS的BUG(一)

    2011年6月,我毕业了.2012年我接触了CSS,本以为会好过些能赚点钱了,可谁知,Internet Explorer(下称IE),这个前端工程师的噩梦浏览器让我不再那么好过了.各种出现在IE身上的 ...