日期:2014-05-16 浏览次数:20609 次
?
1.?jdbc.properties
### oracle ### jdbc.driverclass=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl jdbc.username=scott jdbc.password=tiger?
2.?常量类
import java.util.HashMap; import java.util.Map; public final class JDBCConstants { private JDBCConstants() { } //用于存放jdbc.properties中的内容 public static Map<String, String> jdbcMap = new HashMap<String, String>(4); }
?
3.解析?jdbc.properties
?
import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import org.apache.log4j.Logger; public final class ParseSqlPropertiesFile { private static final Logger LOG = Logger .getLogger(ParseSqlPropertiesFile.class); private static ParseSqlPropertiesFile instance = new ParseSqlPropertiesFile(); private ParseSqlPropertiesFile() { } public static ParseSqlPropertiesFile getInstance() { return instance; } private static final String SQL_FILE_PATH = "jdbc.properties"; /** * @Title: getJDBCConfig * @Description: 获取jdbc.properties中配置的key-value * @return * @author * @date 2011-12-29 */ public Map<String, String> getJDBCConfig() { InputStream is = ParseSqlPropertiesFile.class.getClassLoader() .getResourceAsStream(SQL_FILE_PATH); Properties prop = new Properties(); try { prop.load(is); } catch (IOException e) { LOG.error("load file faile!", e); } catch (Exception e) { LOG.error("load file faile!", e); } Set<Entry<Object, Object>> set = prop.entrySet(); Iterator<Entry<Object, Object>> it = set.iterator(); String key = null, value = null; while (it.hasNext()) { Entry<Object, Object> entry = it.next(); key = String.valueOf(entry.getKey()); value = String.valueOf(entry.getValue()); //放入Map JDBCConstants.jdbcMap.put(key, value); } return JDBCConstants.jdbcMap; } }?