写一个文件:路径(config/jdbc.properties)
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@ip:poot:name
jdbc.username=root
jdbc.password=123
写方法:
public class PropertiesContext {
private static Properties jdbcProperties;
public static Properties getJdbcProperties(){
if(null == jdbcProperties){
InputStream inputStream = PropertiesContext.class.getClassLoader().getResourceAsStream("config/jdbc.properties");
jdbcProperties = new Properties();
try {
jdbcProperties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
return jdbcProperties;
}
}
使用:
private Properties propertiesJdbc = PropertiesContext.getJdbcProperties();
Class.forName(propertiesJdbc.getProperty("jdbc.driverClassName")).newInstance();
String url = propertiesJdbc.getProperty("jdbc.url");
String username = propertiesJdbc.getProperty("jdbc.username");
String password = propertiesJdbc.getProperty("jdbc.password");
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@ip:poot:name
jdbc.username=root
jdbc.password=123
写方法:
public class PropertiesContext {
private static Properties jdbcProperties;
public static Properties getJdbcProperties(){
if(null == jdbcProperties){
InputStream inputStream = PropertiesContext.class.getClassLoader().getResourceAsStream("config/jdbc.properties");
jdbcProperties = new Properties();
try {
jdbcProperties.load(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
return jdbcProperties;
}
}
使用:
private Properties propertiesJdbc = PropertiesContext.getJdbcProperties();
Class.forName(propertiesJdbc.getProperty("jdbc.driverClassName")).newInstance();
String url = propertiesJdbc.getProperty("jdbc.url");
String username = propertiesJdbc.getProperty("jdbc.username");
String password = propertiesJdbc.getProperty("jdbc.password");