dbconfig.properties文件在src根目录下,(文件中的内容都不带双引号,文件中不存在数据类型,只能通过io进行读写,然后根据=进行分割,其实在properties中,使用冒号,空格分割都可以)
dbconfig.properties中的内容为:
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/school?serverTimezone=CST
user=root
password=123456
minPoolSize=5
maxPoolSize=10
package util;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class DBConfig {
private static Properties dbConfig;
static{
try {
dbConfig = new Properties();
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("dbconfig.properties");
//这里程序中运行当前线程来读取properties文件信息,获得输入流,然后将此输入流放入properties对象中,然后再通过properties对象来获取文件中的信息
dbConfig.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getConfig(String key){
return dbConfig.getProperty(key,"");
}
public static String getConfig(String key,String defaultValue){
return dbConfig.getProperty(key,defaultValue);
}
public static Integer getIntegerValue(String key){
return Integer.parseInt(dbConfig.getProperty(key,""));
}
public static Integer getIntegerValue(String key,String defaultValue){
return Integer.parseInt(dbConfig.getProperty(key,defaultValue));
}
}
网友评论