04_Use the resource binder to bind the property configuration file (ResourceBundle.getBundle("xxx.properties"))

Why use a resource binder to bind property configuration files?
First, use ResourceBundle to obtain the properties in xxx.properties (these properties are the properties that need to be used when connecting to the database), which is more efficient and safer than directly writing the information about connecting to the database into the java program. If you need to change the corresponding properties, you only need to modify the corresponding property parameters in the xxx.properties property file, and the Java program does not need to be changed.

1. The comparison between using ResourceBundle to get properties and setting properties in Java source program

1. ResourceBundle get attribute

Resource configuration file (if you need to change the property, you can change it in this JDBC.properties, the following Java source program does not need to be moved) The
insert image description here
java source program (part) when using the resource binder to bind the property configuration file
insert image description here

2. Don’t look at the Java source program that does not use ResourceBundle to obtain attributes.
The code here seems to be a little less than using ResourceBundle to obtain attributes. Imagine that if the step of connecting to the database is encapsulated into a method, we use the mysql database today and Oracle tomorrow. database, and the defined username and password are different,
then we have to constantly modify
the three parameters of conn = DriverManager.getConnection(url, usr, password); url, user and password; and if in some In projects that need to protect database content, we can't let customers directly input passwords into the Java source program when connecting to the database; by using the properties property file, we can allow customers to have a relatively private environment to enter related properties , we are getting the corresponding properties to connect to the database.
insert image description here




2. The usage of code analysis ResourceBundle

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ResourceBundle;

public class ResourceBundleTest {
    
    
    public static void main(String[] args) {
    
    
        //1.使用ResourceBundle 下的 getBundle() 绑定对应的属性配置文件
        //ResourceBundle bundle = ResourceBundle.getBundle("JDBC.properties");
        ResourceBundle bundle = ResourceBundle.getBundle("JDBC");//这里属性配置文件可以不用写后缀

        //2.利用getString()来获取对应属性的值
        String driver = bundle.getString("driver");//com.mysql.cj.jdbc.Driver
        String url = bundle.getString("url");//jdbc:mysql://localhost:3306/test
        String user = bundle.getString("user");//root
        String password = bundle.getString("password");//root
        
        try {
    
    
            //注册驱动(这里开始编写JDBC程序的步骤了)
            Class.forName(driver);
            //获取数据库连接
            //连接数据库所需要的属性都已经从配置文件中获取了,需要修改直接去配置文件中修改即可
            Connection conn = DriverManager.getConnection(url, user, password);
            System.out.println("连接数据库成功:" + conn);//查看是否与数据库连接成功
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }
}

In fact, it is very simple, it is to make a prerequisite preparation for the convenience of connecting to the database in the future.

Guess you like

Origin blog.csdn.net/qq_45657848/article/details/127593635