Java在Web项目中读取properties文件
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文件的更多相关文章
- Java项目中读取properties文件,以及六种获取路径的方法
下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...
- java项目中读取properties文件
这里的配置文件都放在src下面, System.properties的内容 exceptionMapping=exceptionMapping.properties config=config.pro ...
- java web项目中 读取properties 路径的问题
可以先获取项目的classPath String classPath = this.getClass().getResource("/").getPath();//获取classP ...
- 五种方式让你在java中读取properties文件内容不再是难题
一.背景 最近,在项目开发的过程中,遇到需要在properties文件中定义一些自定义的变量,以供java程序动态的读取,修改变量,不再需要修改代码的问题.就借此机会把Spring+SpringMVC ...
- (转)关于java和web项目中的相对路径问题
原文:http://blog.csdn.net/yethyeth/article/details/1623283 关于java和web项目中的相对路径问题 分类: java 2007-05-23 22 ...
- (转)java 从jar包中读取资源文件
(转)java 从jar包中读取资源文件 博客分类: java 源自:http://blog.csdn.net/b_h_l/article/details/7767829 在代码中读取一些资源文件 ...
- java web项目中打开资源文件中文乱码
1 java web项目中经常使用多模块管理.在某一个模块中添加了一些资源文件.但不是启动项目.有时候需要在程序中读取资源文件内容,打包后放到容器中就不能正常运行了.需要将所有资源文件放到启动项目的 ...
- Java之——Web项目中DLL文件动态加载方法
本文转自:https://blog.csdn.net/l1028386804/article/details/53903557 在Java Web项目中,我们经常会用到通过JNI调用dll动态库文件来 ...
- java 从jar包中读取资源文件
在代码中读取一些资源文件(比如图片,音乐,文本等等),在集成环境(Eclipse)中运行的时候没有问题.但当打包成一个可执行的jar包(将资源文件一并打包)以后,这些资源文件找不到,如下代码: Jav ...
随机推荐
- 基于css3的轮播效果
花了一上午来调整页面在ie10上的显示问题,sass编译生成的css文件在ie内核下一直不能正确加载,果然兼容性的问题还需要好好研究.转入正题,用css3实现轮播效果主要是基于css3的framewo ...
- C#中去除字符串空格的三种方法
static void Main() { //demo1 除去空格,提取出各个单词 string s = "a b c"; string[] word = s.Split(new ...
- clojure 之 hello world
1. 安装Leiningen 2. lein new app bar 3. lein run Hello, World!
- google你懂得
地址 https://github.com/racaljk/hosts
- HTTP 响应头信息
HTTP 响应头信息 HTTP请求头提供了关于请求,响应或者其他的发送实体的信息. 在本章节中我们将具体来介绍HTTP响应头信息.
- 当C++多继承遇上类型转换[转]
1 由来 客户用陈旧的VC++6.0进行项目开发,有一块功能需要我来实现.让一个早就习惯了VS2013的人去使用C++支持不太好的VC6去做开发实在是非常不爽,于是另辟蹊径,打算使用VC++201 ...
- appium+Python真机运行测试demo的方法
appium+Python真机运行测试demo的方法 一, 打开手机的USB调试模式 二, 连接手机到电脑 将手机用数据线连接到电脑,并授权USB调试模式.查看连接的效果,在cmd下运行命 ...
- Kmeans++算是DONet实现
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 【Mocha.js 101】Mocha 入门指南
序 说到质量控制,不得不提起测试驱动开发(TDD)和行为驱动开发(BDD).随着敏捷软件开发的推行,软件质量控制的重担也逐渐从测试工程师转向了研发工程师.测试驱动也随之悄然而生,成为了敏捷开发中重要的 ...
- Designing for iOS: Graphics & Performance
http://robots.thoughtbot.com/designing-for-ios-graphics-performance [原文] In the previous article, w ...