写一个文件:路径(config/jdbc.properties)
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.1.1.1:1111:name
jdbc.username=user
jdbc.password=password
写方法:
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");读取properties文件,创建objectFactory
最新推荐文章于 2024-11-04 23:44:57 发布
本文介绍了一种通过配置文件jdbc.properties来设置Oracle数据库连接的方法。包括如何读取配置文件、加载数据库驱动、获取连接URL及认证信息等关键步骤。
1026

被折叠的 条评论
为什么被折叠?



