当前位置:网站首页>读取JDBC配置文件
读取JDBC配置文件
2022-08-04 05:33:00 【Jorge666】
用单例的方式,properties文件在src目录下。
public class ConfigManager {
private static ConfigManager configManager = null;
private static Properties properties = null;
// 私有的构造方法
private ConfigManager() {
properties = new Properties();
// 要想 使用该对象操作文件(类型必须是后缀名.properties),需要加载输入流
InputStream inputStream = ConfigManager.class.getClassLoader().getResourceAsStream("database.properties");
try {
properties.load(inputStream);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 公共的获取实例的方法来创建对象,在这个方法里面控制单例
public static ConfigManager getInstance() {
if (configManager == null) {
configManager = new ConfigManager();
}
return configManager;
}
// 读取配置文件里面的值
public String getValue(String key) {
return (String) properties.get(key);
}
}
配置文件信息 database.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/school?useUnicode=true&characterEncoding=utf-8
user=root
password=1234
边栏推荐
猜你喜欢
随机推荐
基于Webrtc和Janus的多人视频会议系统开发7 - publish和subscribe声音设备冲突导致对方听不到声音
一场聚会,转行渗透测试月薪13.5k,感谢那个女同学......
分布式cache项目
安全漏洞是如何被发现的?
C语言结构体(必须掌握版)
LeetCode_Nov_5th_Week
Question 1000: Input two integers a and b, calculate the sum of a+b, this question is multiple sets of test data
Uos统信系统 SSH
基于Webrtc和Janus的多人视频会议系统开发6 - 从Janus服务器订阅媒体流
用PPAPI插件技术在Web上显示会议视频、桌面、PPT等
vim的介绍
Implementation of CAS lock-free queue
win10下mediasoup搭建过程中的一些坑记录
结构体传参-C语言
指针的运算【C语言】
Shell基础
结构体内存对齐-C语言
[Development Miscellaneous][Editor][Code Reading]ctags & vim
MySQL基础
IDEA中创建web项目实现步骤