配置文件

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. CREATE DATABASE

    CREATE DATABASE IF NOT EXISTS `focusdata` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;USE `fo ...

  2. 命令提示符CMD远程连接Mysql学习笔记

    我想要用Cmd可以像SecureCRT一样远程连接数据库,查询数据,因为用cmd的话可以用批处理,方便脚本调用 第一步:直接使用命令 mysql –h ip –u user –p,本地运行了该命令提示 ...

  3. android 巧用finish方法

    在android应用开发中,我们从一个activity跳到另一个activity时,我么要用到Intent: eg:Intent intent = new Intent(A.this,B.class) ...

  4. Android学习笔记--AlertDialog应用

    1. 自定义实现带图标的TextView IconTextView.java package com.evor.andtest; import android.content.Context; imp ...

  5. Lintcode--010(最长上升子序列)

    给定一个整数序列,找到最长上升子序列(LIS),返回LIS的长度.LIS(longestIncreasingSubsequence) 说明: 最长上升子序列的定义: 最长上升子序列问题是在一个无序的给 ...

  6. 将主机IDS OSSEC日志文件存入MYSQL的方法

    将主机IDS OSSEC日志文件存入MYSQL的方法 http://www.freebuf.com/articles/system/6139.html http://ossec-docs.readth ...

  7. Unity3D中C#编写脚本

    1.继承MonoBehaviour类:任何一个游戏脚本都需要去继承MonoBehaviour这个类,只是在创建javascript脚本的时候,系统会将其类名与继承关系隐藏起来. 2.声明变量:使用Ja ...

  8. CentOs6.5中安装和配置vsftp简明

    这篇文章主要介绍了CentOs6.5中安装和配置vsftp简明教程,需要的朋友可以参考下     一.vsftp安装篇 复制代码代码如下: # 安装vsftpdyum -y install vsftp ...

  9. vs2008包加载失败

    由于安装vs2008sp1后,新建项目报错,解决未遂,于是重装vs2008,装完后又出现包加载失败问题: Microsoft.Data.Entity.Design.Package.MicrosoftD ...

  10. Hive 6、Hive DML(Data Manipulation Language)

    DML主要是对Hive 表中的数据进行操作的(增 删 改),但是由于Hadoop的特性,所以单条的修改.删除,其性能会非常的低所以不支持进行级操作: 主要说明一下最常用的批量插入数据较为常用的方法: ...