I heard that Properties
is legacy in Java.
Is Properties
still used for load configuration of JDBC from a file as in an an old reply?
Without Properties
, how can we do the same? By normal file-read functions and manually parse the files?
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