You can save you db.properties
file to an external fixed location, and access it for later to retrieve your connection properties:
Properties props = new Properties();
FileInputStream in = new FileInputStream("/external/configuration/dir/db.properties");
props.load(in);
in.close();
String driver = props.getProperty("jdbc.driver");
if (driver != null) {
Class.forName(driver) ;
}
String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");
Connection con = DriverManager.getConnection(url, username, password);
Then, on every environment you can have a different copy of your database settings, without having to change your application file (JAR, ER, or whatever).
Sample database connection properties file:
# Oracle DB properties
#jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@localhost:1571:MyDbSID
#jdbc.username=root
#jdbc.password=admin
# MySQL DB properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/MyDbName
jdbc.username=root
jdbc.password=admin
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…