which is the best way to externalize a database configuration on a desktop app?

隐身守侯 提交于 2019-12-02 19:03:28

问题


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");


来源:https://stackoverflow.com/questions/7582784/which-is-the-best-way-to-externalize-a-database-configuration-on-a-desktop-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!