从配置文件中读取数据获取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文件内容格式 ...
随机推荐
- javascript学习教程之---如何从一个tab切换到banner幻灯片的转换2
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- 本地windows下PHP连接远程oracle遇到的诸多问题
任务目的:本地windows下PHP连接远程服务器下的oracle. 必须必须 确定服务器的数据库版本,如果本地的驱动和对方服务器版本不一致,会导致许多报错. 已知的oracle版本 分为 32位的 ...
- jQuery工作原理解析以及源代码示例
jQuery的开篇声明里有一段非常重要的话:jQuery是为了改变javascript的编码方式而设计的.从这段话可以看出jQuery本身并不是UI组件库或其他的一般AJAX类库.jQuery改变ja ...
- 开发纯ndk程序之环境搭配
安装ndk 从安卓官网下载,ndk,双击解压到当前文件夹.建议想装在那个文件夹便解压到那个文件夹,而且文件夹的路径中不要有空格,因为gcc编译的时候会把空格前后两个字符串作为两个文件夹来对待. 使用g ...
- BufferedInputStream
package file; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStrea ...
- JavaWeb学习笔记--跳转方法小结
服务端跳转:1. RequestDispatcher.forward() public void doGet(HttpServletRequest request, HttpServletRespo ...
- Harris Corner(Harris角检测)
在做图像匹配时,常需要对两幅图像中的特征点进行匹配.为了保证匹配的准确性,所选择的特征必须有其独特性,角点可以作为一种不错的特征. 那么为什么角点有其独特性呢?角点往往是两条边缘的交点,它是两条边缘方 ...
- [POJ] 1274 The Perfect Stall(二分图最大匹配)
题目地址:http://poj.org/problem?id=1274 把每个奶牛ci向它喜欢的畜栏vi连边建图.那么求最大安排数就变成求二分图最大匹配数. #include<cstdio> ...
- Delphi - GetUserNameEx(学一下导出Windows API,以及Array Char充当缓冲区的用法,下标必须从零开始)
(* * Author : http://www.michael-puff.de * Date : 2006-03-29 * License : PUBLIC DOMAIN *) function G ...
- ASP.NET MVC4中的Model验证 移除指定验证信息
MVC中通过Model在页面间传值使的程序开发变得更加的快捷,但是很多时候,我们在数据传递的时候为了确保数据的有效性,要对Model的相关属性做基本的数据验证. 不多说直接上个代码,Model的实体类 ...