【问题标题】:Storing configuration related information in Java在 Java 中存储配置相关信息
【发布时间】:2012-03-26 15:57:39
【问题描述】:

我正在使用 Swing 开发 Java 桌面应用程序。由于应用程序以数据为中心,因此我使用 JDBC 与 mySql 连接。我想将数据库连接(connectionString)、用户名和密码存储在单独的文件中。什么是最好的方法?

(在 .net app.config 中存储这些信息)

【问题讨论】:

  • 属性文件可以正常读写操作
  • 参见answer,建议java.util.prefs.Preferences

标签: java swing


【解决方案1】:

您可以将参数存储在属性文件中,例如

hostname=localhost:3306/mydb
username=root
password=pass


将此信息存储在属性文件中,例如 config.properties。您可以在运行时使用 java 中的 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")

【讨论】:

    【解决方案2】:

    您可以为此使用properties file。 Hibernate 已经有了它的properties configuration file。你也可以使用hibernate.cfg.xml

    【讨论】:

      猜你喜欢
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多