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"));
}
}