Wednesday, February 3, 2016

Properties

Example:

public class DBConnector {
 private static String url = null;
 private static String username = null;
 private static String password = null;
 private static String driver = null;
 static{
  Properties pp = new Properties();
  try {
   //pp.load(DBConnector.class.getClassLoader().getResourceAsStream("dbinfo.properties"));
   pp.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("dbinfo.properties")); // this one is better
  } catch (IOException e) {
   //e.printStackTrace();
   throw new RuntimeException(e);
  }
  username = pp.getProperty("username");
  password = pp.getProperty("password");
  driver = pp.getProperty("driver");
  url = pp.getProperty("url");;
 }
 //Other functions
}
For character encoding in properties file: use native2ascii from jdk/bin/, copy the String into native2ascii, it will translate to ascii

No comments:

Post a Comment