Get the database information saved in the Properties file

The content of the file jdbc.properties:
Insert picture description here
Tools:

/**
 * 获取 Properties 文件对象的工具类
 */
public class PropertiesUtil {
    
    
	
	//私有化构造方法 
	private PropertiesUtil() {
    
    

	}

	private static Properties properties = null;

	/**
	 * 不传参数则默认获取 jdbc.properties 文件
	 * @return
	 */
	public static Properties getProperties() {
    
    
		return getProperties("jdbc.properties");
	}

	public static Properties getProperties(String fileName) {
    
    
		if (properties == null) {
    
    
			properties = new Properties();
			try {
    
    
				properties.load(PropertiesUtil.class.getClassLoader().getResourceAsStream(fileName));
			} catch (IOException e) {
    
    
				e.printStackTrace();
			}
		}
		return properties;
	}
}

Guess you like

Origin blog.csdn.net/qq_41504815/article/details/114002439