配置文件

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. ROBOTS.TXT屏蔽笔记、代码、示例大全

    自己网站的ROBOTS.TXT屏蔽的记录,以及一些代码和示例: 屏蔽后台目录,为了安全,做双层管理后台目录/a/xxxx/,蜘蛛屏蔽/a/,既不透露后台路径,也屏蔽蜘蛛爬后台目录 缓存,阻止蜘蛛爬静态 ...

  2. python修改txt文件内容

    ①以r模式打开文件并用readlines方法读入列表l中 ②修改相关行,直接用l[n]形式即可 ③关闭文件 ④以w方式打开文件,用writelines方法写入文件(覆盖文件内容) ⑤关闭文件 需要注意 ...

  3. 深入浅出scanf、getcha、gets、cin函数

    转:问题描述一:(分析scanf()和getchar()读取字符) scanf(), getchar()等都是标准输入函数,一般人都会觉得这几个函数非常简单,没什么特殊的.但是有时候却就是因为使用这些 ...

  4. 【转】Ubuntu 修改hosts

    原文网址:http://l.14551.org/2009/12/2166 Ubuntu系统的Hosts只需修改/etc/hosts文件,在目录中还有一个hosts.conf文件,刚开始还以为只需要修改 ...

  5. NOI2011 兔兔与蛋蛋游戏

    http://www.lydsy.com/JudgeOnline/problem.php?id=2437 这道题真是极好的. 75分做法: 搜索. 出题人真的挺良心的,前15个数据点的范围都很小,可以 ...

  6. cf493C Vasya and Basketball

    C. Vasya and Basketball time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. PHP关于时区问题

    最近在学习PHP过程中发现PHP中的格式化时间戳比北京时间晚了8个小时,上网搜索发现原来是时区不对,解决办法是:      1.永久修改           更改php.ini文件中的data.tim ...

  8. Android 读取手机SD卡根目录下某个txt文件的文件内容

    1.先看activity_main.xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/and ...

  9. 【转】YUV格式&像素

    一幅彩色图像的基本要素是什么? 说白了,一幅图像包括的基本东西就是二进制数据,其容量大小实质即为二进制数据的多少.一幅1920x1080像素的YUV422的图像,大小是1920X1080X2=4147 ...

  10. python入门第一天,循环与判断

    学习一门新的语言最重要的就是练习. 一.脚本需求: 编写登陆接口 输入用户名密码 认证成功后显示欢迎信息 输错三次后锁定 二.脚本流程图: 写代码之前画个流程图总是好的,可以让你理清思路,避免写着写着 ...