which is the best way to externalize a database co

2020-05-08 17:32发布

问题:

I'm working in a desktop app, and we're using Eclipse RCP with EclipseLink.
All my database configuration is inside a class, but I'll need these thing (database URL, password, username) configurable.
Which is the best way to do that?

回答1:

Easiest way would be to use some simple property file and java.util.Properties to read it.

You can read the file from classpath, e.g:

Class.getResourceAsStream ("resource.properties");


回答2:

The standard way is to have a properties (either java.util.Properties or XML) file in which is stored the database details. This can be writeable by the user of course, and passwords stored in such a file need to be encrypted.

It's a nice idea to give the user a means of setting these from the application rather than having to edit the file manually though.



回答3:

In addition to the answers already given I would suggest externalizing the reference to the properties file by specifying it as a command-line option, e.g:

java my.app.MainClass -Ddb.config=/path/to/db.properties

You can then grab the path like so:

final String dbConfigPath = System.getProperty("db.config");