Question

I've a swing application that has to connect to database for some resources, for this i used .properties file to store database properties and that can be read at runtime.
For this i am using the following code

    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.");
       }
   }

but i've to call this method whenever i want to connect to the database (for Connection object). I know the thing that now processing is fast enough to run these lines in micro seconds, but it's for my own knowledge that suggest me the ways through which i can store these DB values when application starts or the first time user try to connect to DB for any operation in such objects or variables or constants that will be usable until the application restarts and can be called directly without reading the file.

P.S. : I know that the DB values will not change oftentimes, and if it happens than i'll be happy to restart my application :)

Was it helpful?

Solution

by making these static fields in a separate class, they will not be loaded until the first time you access URL,USER, or 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);
       }
  }
}

OTHER TIPS

You can nake a check condition which will check if it is first time then set the value other wise use the existing value

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

Here's a generic environment class for you. You can get your DB props like Environment.getEnvironment().getProperty("DB_URL"), etc.

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());
      }
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top