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 ...
随机推荐
- Mysqli封装
<?php //headerheader('content-type:text/html;charset=UTF-8'); class DB { //定义属性 private $ho ...
- CSS实现三角形
突然想起搞一把三角形. 简单来说: 建一个div 宽度.高度设为0 添加两个样式: border 和 border-color 如果需要一个三角形把其他边框都设为透明 transparent < ...
- JQuery的一些简单操作02
一.遍历 1.向下遍历,children.find children只能向下遍历儿子节点的所有元素,find遍历当前元素下面的所有子节点 2.向上遍历,parent,parents,parentsUn ...
- MVC OF UEditor 图片上传- 额外参数 笔记
最近正巧需要用到UEditor ,因为需求,.需要把上传的图片数据上传到阿里云的OSS与记录图片相关信息到MS SQL中 . 不得已只能翻UEditor的实现代码>_<痛苦. 备忘笔记: ...
- 深入理解css系列:清除浮动
如果出现div嵌套,内层元素浮动,而外层包裹的父元素div未设置高度的时候,那么会出现外层不能被撑开的情况. HTML标签代码: <div class="wrap"> ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- UVA 820 --- POJ 1273 最大流
找了好久这两个的区别...UVA820 WA了 好多次.不过以后就做模板了,可以求任意两点之间的最大流. UVA 是无向图,因此可能有重边,POJ 1273是有向图,而且是单源点求最大流,因此改模板的 ...
- Java(六)——抽奖系统
总体思路: 将编号加入ArrayList动态数组中,利用集合的静态方法Collections.shuffle() 乱序集合中的元素从而获得随机数,remove删除已抽编号 代码如下: import ...
- 创建【哆啦A梦】风格字体
学习canvas,为作画.对于一个毫无逻辑思维的人简直遭罪啊~想象坐标坐标坐标啊- - 好啦言归正传,基于本月16号,在春熙路IFS展出120只哆啦a梦,以及canvas的作用,在此介绍一种PS的美化 ...
- ssh整合--struts
一 struts(jar+web.xml+struts.xml+Action) 1import min_jars-------struts-2.3.20.3-all(struts2-blank.war ...