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. Best Coder Round#25 1003 树的非递归访问

    虽然官方解释是这题目里的树看作无向无环图,从答案来看还是在“以1作为根节点”这一前提下进行的,这棵树搭建好以后,从叶节点开始访问,一直推到根节点即可——很像动态规划的“自底向上”. 但这棵树的搭建堪忧 ...

  2. E-Form++图形可视化源码库新增同BCGSoft的Ribbon结合示例

    2015年11月20日,来自UCanCode E-Form++源码库的开发团队消息,E-Form++正式提供了同BCGSoft的Ribbon界面风格相结合的示例,如下图: 下载此示例请访问: http ...

  3. 绝不在构造和析构函数中调用 virtual 函数

    看下面的这段代码,问 print调用的是基类还是派生类的版本? 答案是 基类... 可能大家会很惊讶,print不是virtual function 吗?为什么不是调用派生类的版本呢? 首先,当定义一 ...

  4. Tips for VNCServer config

    Tips for VNCServer After the ClearCase server reboot by Jingwei, my vncserver background process is ...

  5. PHP开发调试环境配置(基于wampserver+Eclipse for PHP Developers )

    1 软件准 WampServer 下载地址:http://www.wampserver.com/en/#download-wrapper    我下的是 里面包含了搭建PHP必须的4个软件:   1. ...

  6. sublime 3 安装配置

    Sublime Text 3安装与使用   本文是Sublime Text 全程指引 by Lucida (http://www.cnblogs.com/figure9/p/sublime-text- ...

  7. cell 内部 设置width 总不对

    今天 在Cell 里设置屏幕宽 如果 在layoutSubviews 使用 self.width(自己写的分类) 或者 self.view.size.width  都可以,这里 4 4s 5 5s 都 ...

  8. 分享一些DICOM数据下载网站

    医院的DICOM数据轻易不让拷出来,所以这里列出一些医学数据,但不局限于DICOM数据网址,供大家下载. 斯坦福大学体数据库(RAW DATA) http://graphics.stanford.ed ...

  9. Entity Framework search sequnce

    sql express, then (LocalDb)\v11.0 (LocalDb)\mssqllocaldb sqllocaldb i  could list all the local db i ...

  10. 在RedHat上安装gcc,java 和 eclipse-rcp

    本文全是如何用rpm包在红帽子54上安装gcc,automake,java和eclipse等,不是源代码编译,请大家不要误会了. 其实通过rpm包安装东西很简单,麻烦的是有很多rpm是要根据顺序进行先 ...