从配置文件中读取数据获取Connection
配置文件
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的更多相关文章
- Feign从配置文件中读取url
Feign的url和name都是可配置的,就是从配置文件中读取的属性值,然后用占位符引用就可以了: ${rpc.url} @FeignClient(name = "me", url ...
- spring boot: 从配置文件中读取数据的常用方法(spring boot 2.3.4)
一,从配置文件中读取数据有哪些方法? 通常有3种用法: 1,直接使用value注解引用得到配置项的值 2, 封装到Component类中再调用 3, 用Environment类从代码中直接访问 生 ...
- 【Python学习笔记七】从配置文件中读取参数
将一些需要更改或者固定的内容存放在配置文件中,通过读取配置文件来获取参数,这样修改以及使用起来比较方便 1.首先是配置文件的写法如下一个environment.ini文件: 里面“[]”存放的是sec ...
- spring boot 项目从配置文件中读取maven 的pom.xml 文件标签的内容。
需求: 将pom.xml 文件中的版本号读取到配置文件并打印到日志中. 第一步: 在pom.xml 中添加以下标签. 第二步: 将version 标签的值读取到配置文件中 这里使用 @@ 而不是 ...
- 监听tomcat服务器启动/关闭并从配置文件中读取参数进行初始化
监听tomcat服务器启动/关闭很简单(2步): 1. 建立一个类实现ServletContextListener接口,重写其中的方法(contextDestroyed和contextInitiali ...
- C# 从配置文件中读取/写入信息
读取: var currMemberID = System.Configuration.ConfigurationManager.AppSettings["tolunaMemberID&qu ...
- unittest(13)- 从配置文件中读取测试数据
case.config # 1. http_request.py import requests class HttpRequest: def http_request(self, url, meth ...
- 在ASP.NET 5中读取配置文件
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 在ASP.NET 5中摒弃了之前配置文件的基础结构,引入了一个全新配置文件系统.今天推荐的文 ...
- java 如何从配置文件(.properties)中读取内容
1.如何创建.properties文件 很简单,建立一个txt文件,并把后缀改成.properties即可 2.将.properties文件拷入src的根目录下 3..properties文件内容格式 ...
随机推荐
- 使用PHP对数据库输入进行恶意代码清除
这是一个有用的PHP函数清理了所有的输入数据,并删除代码注入的几率. function sanitize_input_data($input_data) { $input_data = trim(ht ...
- php简单的爬虫
爬虫的原理是分析下载的页面,找出其中的连接,然后再下载这些链接,对链接再进行更深层次的递归,周而复始.在数据存储方面,先存储到redis里面,再有redis 写入到mysql,这样可以减轻mysql写 ...
- quartz spring
简单例子可参考 http://yangpanwww.iteye.com/blog/797563 http://liuzidong.iteye.com/blog/1118992 关于时间配置可参考另一篇 ...
- Jmeter实现WebSocket协议的接口和性能测试方法
WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器全双工通信(full-duplex). 浏览器和服务器只需要要做一个握手的动作,然后,浏览器和服务器之间就形成了一 ...
- VC2013 添加库文件
1.项目--〉属性--〉链接器 1. You #include the header file (.h) file in your project as necessary. 2. You lis ...
- Spring AOP 原理
AOP将应用系统分为两部分,核心业务逻辑(Core businessconcerns)及横向的通用逻辑,也就是所谓的方面Crosscutting enterprise concerns,例如,所有大中 ...
- Pythonic到底是什么玩意儿?
http://blog.csdn.net/gzlaiyonghao/article/details/2762251 作者:Martijn Faassen 译者:赖勇浩(http://blog.csdn ...
- cf B. Dima and To-do List
http://codeforces.com/contest/366/problem/B 从0到k枚举起点,然后i+k判断是不是i+k>=n如果是i=(i+k)%n;否则i=i+k; #inclu ...
- #if defined 的意思?
在读s3c2440a的test程序,其中option.h文件中有段语句为: #define LCD_N35 //#define LCD_L80 //#define LCD_T35 //#define ...
- VMware Workstation 精致汉化系列 使用方法
http://kuai.xunlei.com/d/QqGABAKChQBwMzxR983 迅雷快传 XP系统之家-温馨提示: VMware Workstation 精致汉化系列 使用方法:1.安装 ...