我正在使用Swing開發Java桌面應用程序。由於應用程序是以數據爲中心的,因此我正在使用JDBC與mySql進行連接。我想在單獨的文件中存儲數據庫連接(connectionString),用戶名和密碼。什麼是最好的方法呢?以Java存儲配置相關信息
(在.NET app.config中存儲這些信息)
我正在使用Swing開發Java桌面應用程序。由於應用程序是以數據爲中心的,因此我正在使用JDBC與mySql進行連接。我想在單獨的文件中存儲數據庫連接(connectionString),用戶名和密碼。什麼是最好的方法呢?以Java存儲配置相關信息
(在.NET app.config中存儲這些信息)
您可以存儲的參數在屬性文件中像
hostname=localhost:3306/mydb
username=root
password=pass
存儲這些信息的屬性文件喜歡說config.properties 。您可以使用Properties
類的Java
Properties prop = new Properties();
prop.load(new FileInputStream("config.properties"));
String connectionString = prop.getProperty("hostname")
String username = prop.getProperty("username")
String password = prop.getProperty("password")
您可以使用一個properties file運行時加載Java中的性能。 Hibernate已經有其properties configuration file。您也可以使用hibernate.cfg.xml。
屬性文件可以正常工作讀寫操作 – clevertension 2012-03-09 06:47:51
另請參閱本文提供的'java.util.prefs.Preferences' [[]](http://stackoverflow.com/a/9481515/230513)。 – trashgod 2012-03-09 07:06:43