import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties; import javax.sql.DataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; public class JDBCUtils
{
protected static DataSource ds; static
{
try
{
Properties properties = loadPropertyFile("druid.properties");
ds = DruidDataSourceFactory.createDataSource(properties);
}
catch (Exception e)
{
e.printStackTrace();
}
} public static Properties loadPropertyFile(String fileName)
{
if (null == fileName || fileName.equals(""))
{
throw new IllegalArgumentException("Properties file path can not be null: " + fileName);
} InputStream inputStream = null;
Properties properties = null;
try
{
inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream(fileName);
properties = new Properties();
properties.load(inputStream);
}
catch (FileNotFoundException e)
{
throw new IllegalArgumentException("Properties file not found: " + fileName);
}
catch (IOException e)
{
throw new IllegalArgumentException("Properties file can not be loading: " + fileName);
}
finally
{
try
{
if (inputStream != null)
{
inputStream.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
return properties;
} /**
* 获得数据源
*
* @return
*/
public static DataSource getDataSource()
{
return ds;
} public static void closeConnection() throws SQLException
{
if (ds != null && ds.getConnection() != null && !ds.getConnection().isClosed())
{
ds.getConnection().close();
}
}
}

Java在Web项目中读取properties文件的更多相关文章

  1. Java项目中读取properties文件,以及六种获取路径的方法

    下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...

  2. java项目中读取properties文件

    这里的配置文件都放在src下面, System.properties的内容 exceptionMapping=exceptionMapping.properties config=config.pro ...

  3. java web项目中 读取properties 路径的问题

    可以先获取项目的classPath String classPath = this.getClass().getResource("/").getPath();//获取classP ...

  4. 五种方式让你在java中读取properties文件内容不再是难题

    一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...

  5. (转)关于java和web项目中的相对路径问题

    原文:http://blog.csdn.net/yethyeth/article/details/1623283 关于java和web项目中的相对路径问题 分类: java 2007-05-23 22 ...

  6. (转)java 从jar包中读取资源文件

    (转)java 从jar包中读取资源文件 博客分类: java   源自:http://blog.csdn.net/b_h_l/article/details/7767829 在代码中读取一些资源文件 ...

  7. java web项目中打开资源文件中文乱码

    1 java web项目中经常使用多模块管理.在某一个模块中添加了一些资源文件.但不是启动项目.有时候需要在程序中读取资源文件内容,打包后放到容器中就不能正常运行了.需要将所有资源文件放到启动项目的 ...

  8. Java之——Web项目中DLL文件动态加载方法

    本文转自:https://blog.csdn.net/l1028386804/article/details/53903557 在Java Web项目中,我们经常会用到通过JNI调用dll动态库文件来 ...

  9. java 从jar包中读取资源文件

    在代码中读取一些资源文件(比如图片,音乐,文本等等),在集成环境(Eclipse)中运行的时候没有问题.但当打包成一个可执行的jar包(将资源文件一并打包)以后,这些资源文件找不到,如下代码: Jav ...

随机推荐

  1. 面向对象to1

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. React学习——ListView组件

    (草稿) 先把代码放上来,再补充说明 <!DOCTYPE html> <html> <head> <title>React ListView</t ...

  3. BZOJ 3884 欧拉定理 无穷幂取模

    详见PoPoQQQ的博客.. #include <iostream> #include <cstring> #include <cstdio> #include & ...

  4. UVA 1151二进制枚举子集 + 最小生成树

    题意:平面上有n个点(1<=N<=1000),你的任务是让所有n个点连通,为此, 你可以新建一些边,费用等于两个端点的欧几里得距离的平方.另外还有q(0<=q<=8)个套餐(数 ...

  5. SQLAutoCode - error when attempting to generate schema

    I'm trying to auto generate a schema for use in SQLalchemy, I'm using sqlautocode to do this, I use ...

  6. Android中常见功能包描述

    在Android中,各种包写成android.*的方式,重要包的描述如下所示:android.app :提供高层的程序模型.提供基本的运行环境android.content:包含各种的对设备上的数据进 ...

  7. linux awk的使用

    awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报告时,显得尤为强大.简单来说awk就是把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行各 ...

  8. javascript数组 去重

    数组去重的方法有很多,到底哪种是最理想的,自己不清楚.于是自己测试了下数组去重的效果和性能.测试十万个数据,代码和所耗大概时间如下. 到底采用哪种方法,根据实际情况而定吧. /*方法一: 1,'1' ...

  9. 【php学习】PHP 入门经典第一章笔记

    第一章: php在线手册:http://php.net/manual/zh/index.php 在开始学习PHP之前,先来看一个合格的PHP程序员今后应具备哪些知识,这里只是笔者的一些总结,希望对读者 ...

  10. Entity Framework关联查询以及数据加载(延迟加载,预加载)

    数据加载分为延迟加载和预加载 EF的关联实体加载有三种方式:Lazy Loading,Eager Loading,Explicit Loading,其中Lazy Loading和Explicit Lo ...