0

我有一个摆动应用程序,它必须连接到数据库以获得一些资源,为此我使用.properties文件来存储数据库属性并且可以在运行时读取。
为此,我使用以下代码

    public void readPropertiesFile(){
       try{
         InputStream is = ReadValues.class.getResourceAsStream(PROP_FILE);
         Properties prop = new Properties();
         prop.load(is);
         String URL = prop.getProperty("DB_URL");
         String user = prop.getProperty("DB_USER");
         String  pwd = prop.getProperty("DB_PWD");
         is.close();
      /* code to use values read from the file*/
       }catch(Exception e){
         System.out.println("Failed to read from " + PROP_FILE + " file.");
       }
   }

但是每当我想连接到数据库(对于Connection对象)时,我都必须调用此方法。我知道现在处理速度足够快,可以在微秒内运行这些行,但据我所知,当应用程序启动或用户第一次尝试连接到 DB 时,我可以通过这些方式存储这些 DB 值对于这样objectsvariables那样的任何操作,constants在应用程序重新启动之前都可以使用,并且可以直接调用而无需读取文件。

PS:我知道数据库值不会经常更改,如果发生这种情况,我会很乐意重新启动我的应用程序 :)

4

3 回答 3

2

通过将这些静态字段放在单独的类中,直到您第一次访问 URL、USER 或 PASSWORD 时才会加载它们。

public class DbProps {
  public static final String URL;
  public static final String USER;
  public static final String PASSWORD;

  static {
       try{
         InputStream is = ReadValues.class.getResourceAsStream(PROP_FILE);
         try {
           Properties prop = new Properties();
           prop.load(is);
           URL = prop.getProperty("DB_URL");
           USER = prop.getProperty("DB_USER");
           PASSWORD = prop.getProperty("DB_PWD");
         } finally {
           is.close();
         }
       }catch(Exception e){
         throw new RuntimeException("Failed to read from " + PROP_FILE + " file.", e);
       }
  }
}
于 2012-01-05T14:38:07.280 回答
2

您可以创建一个检查条件,该条件将检查是否是第一次,然后设置值,否则使用现有值

public static boolean isFirstTime = true;
public static String URL = true;
public static String user = true;
public static String pwd = true;
public void readPropertiesFile(){
if(isFirstTime){
       try{

         InputStream is = ReadValues.class.getResourceAsStream(PROP_FILE);
         Properties prop = new Properties();
         prop.load(is);
         URL = prop.getProperty("DB_URL");
         user = prop.getProperty("DB_USER");
         pwd = prop.getProperty("DB_PWD");
isFirstTime = false;
         is.close();
      /* code to use values read from the file*/
       }catch(Exception e){
         System.out.println("Failed to read from " + PROP_FILE + " file.");
       }
}
   }
//use this URL user and pwd in your application
于 2012-01-05T14:44:01.403 回答
2

这是适合您的通用环境类。你可以得到你的数据库道具,比如Environment.getEnvironment().getProperty("DB_URL")等。

public class Environment {
   private static final String PROP_FILE = "somefilename";

   private static final Environment singleton = new Environment();

   public static Environment getEnvironment() {
      return singleton;
   }

   private Properties properties = new Properties();

   protected Environment() {
      super();
      loadProperties();
   }

   public Properties getProperties() {
      return properties;
   }

   public String getProperty(String propertyName) {
      return getProperty(propertyName, System.getProperty(propertyName));
   }

   public String getProperty(String propertyName, String defaultValue) {
      return getProperties().getProperty(propertyName, defaultValue);
   }

   public void loadProperties() {
      URL resourceURL = null;

      try {
         resourceURL = Thread.currentThread().getContextClassLoader()
               .getResource(PROP_FILE);
         getProperties().load(resourceURL.openStream());
         System.out.println("Loaded properties from "
               + resourceURL.toExternalForm());
      } catch (IOException ioe) {
         System.err.println("Failed to load properties from "
               + resourceURL.toExternalForm());
      }
   }
}
于 2012-01-05T14:45:33.140 回答