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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - 学海无涯

在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 }