0

私はいくつかのリソースのためにデータベースに接続する必要があるswingアプリケーションを持ってい.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 値を保存する方法を提案するのは、私自身の知識のためです。アプリケーションが再起動するまで使用でき、ファイルを読み取らずに直接呼び出すことができる、objectsまたはvariablesまたはなどの操作。constants

PS : DB の値が頻繁に変更されることはないことはわかっています。変更が発生した場合は、喜んでアプリケーションを再起動します :)

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")などの DB 小道具を取得できます。

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 に答える