从配置文件中读取数据获取Connection

配置文件

db.driver=com.mysql.jdbc.Driver
db.url=jdbc\:mysql\://localhost\:3306/mybase
db.user=root
db.pswd=[email protected]

#-- 连接池初始化连接数 --
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;
    }
}
时间: 2024-08-01 23:28:15

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

jdbc基础 (二) 从配置文件中读取连接数据库

上一篇描述了对mysql数据库的简单操作,下面来看一下开发中应该如何灵活应用. 因为jdbc对数据库的驱动加载.连接获取.释放资源的代码都是相同的,为了提高代码的复用性,我们可以写一个工具类,将数据库驱动加载.获取连接.资源释放的代码封装起来.同时,为了提高工具类的灵活性,可以将数据库的驱动.url.用户名.密码等信息以键值对的形式存放在properties文件中,工具类初始化时从配置文件中读取所要连接数据库的信息.当需要更改连接的数据库时,只需要更改配置文件即可,而不必改写工具类的代码. 下面

SpringBoot 中从yml配置文件中读取常用的参数值

SpringBoot现在基本上都是使用application-XXX.yml(生产环境为pro,开发测试环境为dev)来配置项目中的一些配置条件,在springboot中还可以通过从yml文件中将yml中的数据直接读取出来. 1.yml文件(这里设置参数的时候往往设置两层前缀,避免在调用的时候该属性中的部分参数名与计算机中的某些名字冲突) test: person: lastName: hello age: 18 boss: false birth: 2017/12/12 maps: {k1:

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

监听tomcat服务器启动/关闭很简单(2步): 1. 建立一个类实现ServletContextListener接口,重写其中的方法(contextDestroyed和contextInitialized). package action; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class SystemListener implements Se

【SSM整合】spring配置文件中读取配置文件的三种方式

目录 依赖 jdbc.properties applicationContext.xml(spring配置文件) 依赖 <!--mysql--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <

从配置文件中读取变量

从配置文件或数据库中读取信息并缓存

在项目中将一些常用的不变的信息可以缓存起来,这样可以减少内存的使用率,提高性能.比如说就数据库连接的相关信息等,可以用单例模式第一次进行连接的时候将数据库连接的相关信息缓存再单例对象中. 首先建个.properties文件,存放数据库连接信息,内容如下: #数据库配置 driver_class = oracle.jdbc.driver.OracleDriver url= jdbc:oracle:thin:@192.168.20.188:1521:orcl username = qwszpt pa

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

(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 在ASP.NET 5中摒弃了之前配置文件的基础结构,引入了一个全新配置文件系统.今天推荐的文章就简单介绍了这方面的内容. 微软在即将正式发布的跨平台运行时当中引入了一个全新的配置文件基础结构,基于这个配置系统,从多个配置文件中读取内容从来没有这么容易过. 这个配置系统不仅内置支持json,xml和ini格式的配置文件,还支持从环境变量中读取内容.如下: var configuration = new

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

1.如何创建.properties文件 很简单,建立一个txt文件,并把后缀改成.properties即可 2.将.properties文件拷入src的根目录下 3..properties文件内容格式 #注释 key=value key2=value1 4.操作代码 1 /* 2 * 从配置文件中读取字段 3 */ 4 public String GetValueByProperties(String KeyStr) { 5 String result = ""; 6 try { 7

IT项目中使用 json格式数据 保存项目配置信息, 在配置文件再读取json文件的内容进行赋值

json格式小巧玲珑,适合做配置文件,特别是大型项目中, 可以将配置信息分类保存到不同的json文件中, 然后再在配置文件中读取配置文件的数据进行赋值, 这里以python为例进行说明: 假设在yourfile.json文件中保存了如下信息: { "user_message":{ "name":"lowman", "age":"14", }, "database":{ "USE