java中配置文件的用法

首先在java中创建配置文件有两种方式

一:可以使用创建文本文档的方式创建将后缀名改为properties文件

二:可以在eclipse中直接创建new-file文件命名为*.properties文件

*.properties文件中写的是链接数据库的属性。文件的内容是键-值得格式

其中:encrypt.algorithm是加密属性

用一个例子说明properties的使用

public static Connection getConnection() {
Connection con = null;//创建链接

Properties properties = new Properties();
//Thread.currentThread()获取当前运行的线程currentThread()是static的,只是返回当前线程对象。
InputStream in = Thread.currentThread().getClass().getResourceAsStream("/database.properties");
try {
//properties.load(new FileInputStream("/database.properties"));
properties.load(in);

String driver = properties.getProperty("driver");

if(driver != null){
System.setProperty("jdbc.drvers", driver);
}

String url = properties.getProperty("jdbc.url");
String username = properties.getProperty("jdbc.username");
String password = properties.getProperty("jdbc.password");
try {
con = DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}


return con;


}
}


如果*.properties文件直接放在src目录下在获取文件路径的时候可以使用/database.properties

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值