您的位置:首页 > 运维架构

Properties文件使用演示

2020-01-15 11:54 1506 查看

jdbc.properties文件

连接数据库的配置文件

driver=com.mysql.jdbc.Driver #连接数据库的驱动器
url=jdbc:mysql://localhost:3306/mydb #连接数据库的路径
username=root  #数据库的用户名
password=123456   #数据库的密码

Properties文件使用演示

package com.hashtable;

import java.io.FileInputStream;
import java.util.Properties;
import java.util.ResourceBundle;

import org.junit.Test;

// Properties使用演示
public class PropertiesTest {
// 加载properties属性文件方式三
@Test
public void test04() {
ResourceBundle rb = ResourceBundle.getBundle("jdbc");//文件名为jdbc.properties
String driver = rb.getString("driver");
String url = rb.getString("url");
System.out.println(driver);
System.out.println(url);
}

// 加载properties属性文件方式二
@Test
public void test03() throws Exception {
Properties prop = new Properties();
prop.load(PropertiesTest.class.getClassLoader()
.getResourceAsStream("jdbc.properties"));
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
System.out.println(driver);
System.out.println(url);
}

// 加载properties属性文件方式一
@Test
public void test02() throws Exception {
Properties prop = new Properties();
prop.load(new FileInputStream("src/jdbc.properties"));
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
System.out.println(driver);
System.out.println(url);
}

@Test
public void test01() {
Properties prop = new Properties();
prop.setProperty("aa", "hello");
prop.setProperty("bb", "world");
System.out.println(prop.getProperty("aa"));
System.out.println(prop.getProperty("bb"));
}
}
  • 点赞
  • 收藏
  • 分享
  • 文章举报
一梦如意 发布了92 篇原创文章 · 获赞 1 · 访问量 1038 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: