原文地址:http://www.open-open.com/lib/view/open1378288465250.html
首先感谢一下。因为以后可能会涉及到hive迁移问题,所以要使用配置文件形式来获取url。
</pre><pre name="code" class="java">import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
public class PropManager {
/**
* @param args
*/
public static void main(String[] args) {
String jdbcUrl = PropManager.getInstance().getProperty("jdbc.cms.url");
String jdbcUsername = PropManager.getInstance().getProperty("jdbc.cms.username");
String jdbcPassword = PropManager.getInstance().getProperty("jdbc.cms.password");
System.out.println("jdbcUrl:"+jdbcUrl);
System.out.println("jdbcUsername:"+jdbcUsername);
System.out.println("jdbcPassword:"+jdbcPassword);
System.out.println(PropManager.getInstance());
System.out.println(PropManager.getInstance());
}
private PropManager() {
}
private static PropManager propManager;
public synchronized static PropManager getInstance() {
if (propManager == null) {
propManager = new PropManager();
}
return propManager;
}
public static CompositeConfiguration config = new CompositeConfiguration();
static {
try {
config.addConfiguration(new PropertiesConfiguration("jdbc.properties"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
public String getProperty(String key)
{
return config.getString(key);
}
}
使用的包: commons-configuration-1.6.jar
几点说明:
1. 采用了单态设计模式,
2.属性文件在classpath目录下.
3. 采用commons-configuration-1.6.jar来读取属性文件