示例:使用 db.properties 文件配置数据库连接属性,并实现对MySQL数据库的连接。
(1)在项目的默认路径(src目录)下创建 db.properties 属性文件,并编写MySQL数据连接的相关配置信息。
DRIVER_CLASS=com.mysql.cj.jdbc.Driver
DB_URL=jdbc:mysql://localhost:3306/db_admin?serverTimezone=Hongkong&useUnicode=true&characterEncoding=utf8&useSSL=false
DB_USER=root
DB_PASSWORD=123456
(2)编写获取 db.properties 属性文件信息,并实现对MySQL数据库的连接。
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
/**
* 使用 db.properties 文件配置数据库连接属性,<br />
* 并实现对MySQL数据库的连接
*
* @author pan_junbiao
*
*/
public class ConnTest
{
private static String DRIVER_CLASS; // 数据库驱动
private static String DB_URL; // 数据库连接地址
private static String DB_USER; // 数据库用户名称
private static String DB_PASSWORD; // 数据库用户密码
public static void main(String[] args)
{
try
{
// 创建Properties类对象
Properties properties = new Properties();
// 读取properties属性文件到输入流中
InputStream is = PropertiesTest.class.getResourceAsStream("/db.properties");
// 从输入流中加载属性列表
properties.load(is);
// 获取数据库连接属性值
DRIVER_CLASS = properties.getProperty("DRIVER_CLASS");
DB_URL = properties.getProperty("DB_URL");
DB_USER = properties.getProperty("DB_USER");
DB_PASSWORD = properties.getProperty("DB_PASSWORD");
// 加载数据库驱动类
Class.forName(DRIVER_CLASS);
System.out.println("数据库驱动加载成功");
// 获取数据库连接对象
Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
System.out.println("数据库连接成功");
} catch (ClassNotFoundException cnfe)
{
cnfe.printStackTrace();
} catch (SQLException sqle)
{
sqle.printStackTrace();
} catch (Exception ex)
{
ex.printStackTrace();
}
}
}
执行结果:执行结果:
3、总结
(1)读取 .properties 属性文件时,使用 Class 对象的 getResourceAsStream()方法,把指定的属性文件读入输入流中,并使用 Properties 类中的 load() 方法,从输入流中读取属性列表(键/值对)。
(2)在使用 Class 对象的 load() 方法加载 .properties 属性文件的输入流后,数据在内存中是以 Hashtable 的形式进行保存的。
(3)使用 Properties 类的 getProperty() 方法,通过 key 键读取出 value 值。