2012-01-05 107 views
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用於objectsvariablesconstants中的任何操作,這些操作在應用程序重新啓動之前可用,並且可以在不讀取文件的情況下直接調用。

P.S. :我知道DB值不會經常變化,如果發生的事情比我會很樂意重新啓動我的應用程序:)

回答

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); 
     } 
    } 
} 
+0

整潔簡單..謝謝! :) – Asif 2012-01-05 14:50:50

+0

我創建了一個類「常量」來保存應用程序中的所有靜態常量值,其中當DBProps類首次初始化應用程序的啓動時,我將存儲這些值,然後將進一步利用它。 – Asif 2012-01-05 14:55:33

+0

put classpath上正確的類? – jtahlborn 2012-01-05 17:45:16

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 
+0

謝謝...... :) – Asif 2012-01-05 14:56:45

2

這兒有你一個通用的環境類。你可以得到你的DB道具如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()); 
     } 
    } 
} 
+0

同樣的事情,我正在與'恆常'類的我..感謝您的建議:) – Asif 2012-01-05 15:00:57