配置文件
dbURL=jdbc:oracle:thin:@10.0.19.252:1521:orcl
dbDriver=oracle.jdbc.driver.OracleDriver
username=moto
password=123456
解析
import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
public class DbInfo {
private static DbInfo dbInfo;
private String dbDriver;
private String dbURL;
private String username;
private String password;
private DbInfo(){}
public static DbInfo instance() throws Exception{
if(dbInfo == null){
dbInfo = new DbInfo();
dbInfo.init();
}
return dbInfo;
}
private void init() throws Exception{
Properties prop = new Properties();
String path = DbInfo.class.getResource("/").getPath() + "db.properties";
prop.load(new FileInputStream(new File(path)));
this.dbDriver = prop.getProperty("dbDriver");
this.dbURL = prop.getProperty("dbURL");
this.username = prop.getProperty("username");
this.password = prop.getProperty("password");
}
public String getDbDriver() {
return dbDriver;
}
public String getDbURL() {
return dbURL;
}
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
}
测试
import com.icss.util.DbInfo
public class Test {
public static void main(String[] args) {
try {
DbInfo dbInfo = DbInfo.instance()
System.out.println(dbInfo.getDbDriver())
System.out.println(dbInfo.getDbURL())
System.out.println(dbInfo.getUsername())
System.out.println(dbInfo.getPassword())
} catch (Exception e) {
e.printStackTrace()
}
}
}