惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
宝玉的分享
宝玉的分享
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
人人都是产品经理
人人都是产品经理
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
博客园 - 【当耐特】
小众软件
小众软件
博客园 - Franky
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
雷峰网
雷峰网
The Cloudflare Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
量子位
Last Week in AI
Last Week in AI
博客园_首页
月光博客
月光博客
IT之家
IT之家
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
Stack Overflow Blog
Stack Overflow Blog
腾讯CDC
云风的 BLOG
云风的 BLOG
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
Recent Commits to openclaw:main
Recent Commits to openclaw:main
D
Docker
The Last Watchdog
The Last Watchdog
有赞技术团队
有赞技术团队
Hacker News - Newest:
Hacker News - Newest: "LLM"
D
DataBreaches.Net
S
Security @ Cisco Blogs
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
TaoSecurity Blog
TaoSecurity Blog
S
Security Affairs
Y
Y Combinator Blog
O
OpenAI News
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
K
Kaspersky official blog
Cloudbric
Cloudbric

博客园 - 学海无涯

在CentOS上安装和部署Shiny Server Hibernate实体生成JSON的问题及解决 在CentOS上安装并运行SparkR CentOS 6主机上的RStudio Server安装步骤 Oracle用户密码过期后重置SYS用户密码 在CentOS中将/var等已有目录挂载到新添加的硬盘 CentOS中的常用命令 Druid连接池初探 安装和配置CentOS时钟同步服务 在CentOS中安装中文支持 在CentOS 6.x中支持exfat格式的U盘(移动硬盘) CentOS常见问题 CentOS MySQL 配置 显示远程网站上的图片 【转载】我们什么时候结婚 纪念新生命诞生 真爱的四个阶段 【转贴词解】富士山下 与寂寞有染,与爱情无关
Java在Web项目中读取properties文件
学海无涯 · 2014-09-15 · via 博客园 - 学海无涯

Posted on 2014-09-15 18:30  学海无涯  阅读(3151)  评论()    收藏  举报

 1 import java.io.FileNotFoundException;
 2 import java.io.IOException;
 3 import java.io.InputStream;
 4 import java.sql.SQLException;
 5 import java.util.Properties;
 6 
 7 import javax.sql.DataSource;
 8 
 9 import com.alibaba.druid.pool.DruidDataSourceFactory;
10 
11 public class JDBCUtils
12 {
13     protected static DataSource ds;
14 
15     static
16     {
17         try
18         {
19             Properties properties = loadPropertyFile("druid.properties");
20             ds = DruidDataSourceFactory.createDataSource(properties);
21         }
22         catch (Exception e)
23         {
24             e.printStackTrace();
25         }
26     }
27 
28     public static Properties loadPropertyFile(String fileName)
29     {
30         if (null == fileName || fileName.equals(""))
31         {
32             throw new IllegalArgumentException("Properties file path can not be null: " + fileName);
33         }
34 
35         InputStream inputStream = null;
36         Properties properties = null;
37         try
38         {
39             inputStream = JDBCUtils.class.getClassLoader().getResourceAsStream(fileName);
40             properties = new Properties();
41             properties.load(inputStream);
42         }
43         catch (FileNotFoundException e)
44         {
45             throw new IllegalArgumentException("Properties file not found: " + fileName);
46         }
47         catch (IOException e)
48         {
49             throw new IllegalArgumentException("Properties file can not be loading: " + fileName);
50         }
51         finally
52         {
53             try
54             {
55                 if (inputStream != null)
56                 {
57                     inputStream.close();
58                 }
59             }
60             catch (IOException e)
61             {
62                 e.printStackTrace();
63             }
64         }
65         return properties;
66     }
67 
68     /**
69      * 获得数据源
70      * 
71      * @return
72      */
73     public static DataSource getDataSource()
74     {
75         return ds;
76     }
77 
78     public static void closeConnection() throws SQLException
79     {
80         if (ds != null && ds.getConnection() != null && !ds.getConnection().isClosed())
81         {
82             ds.getConnection().close();
83         }
84     }
85 }