JDBC数据库连接池的实现:第一天:数据源配置解析工具
版权声明:原创作品,如需转载,请与作者联系。否则将追究法律责任。 |
JDBC数据库连接池的实现:第一天:数据源配置解析工具
开源的连接池很多,实现都不错,可是就不知道怎么做的,在研读了javax.sql包中的API规范后,想手动实现一个通用的JDBC数据库连接池。这个工作量比较大,主要目的是为了深入底层学习Java处理数据库的技术。
第一天:数据源配置解析工具
这个解析工具支持xml配置和properties配置,程序不依赖任何jdk以外的任何第三方包。
xml和properties文件的格式对名字有限制,xml还对格式有限制,要求符合sun公司的http://java.sun.com/dtd/properties.dtd规范。
解析工具要完成的功能,读取配置文件,并存放到Map中,对于非必须而没有配置的属性,给以默认的属性值。
要求:两种配置文件读取方法通用,不进行切换。有导出配置文件的功能。
实现代码:
package com.blog51cto.lavasoft.ds; import java.util.*; import java.util.regex.Pattern; import java.util.regex.Matcher; import java.io.InputStream; /** * Created by IntelliJ IDEA. * User: leizhimin * Date: 2008-3-19 23:22:38 * Company: LavaSoft(http://lavasoft.blog.51cto.com) * 数据源配置工具 */ public class DataSourceConfig { private static Map<String, String> defcfgmap; //初始化默认配置参数 static { defcfgmap = new HashMap<String, String>(); defcfgmap.put("driverClassName", ""); defcfgmap.put("url", ""); defcfgmap.put("user", ""); defcfgmap.put("password", ""); defcfgmap.put("type", "javax.sql.DataSource"); defcfgmap.put("initCreate", "12"); defcfgmap.put("maxIdle", "8"); defcfgmap.put("maxWait", "10"); defcfgmap.put("maxActive", "5"); defcfgmap.put("validationQuery", ""); } /** * 获取数据源配置信息,本方法接受两种配置文件类型,xml和properties类型. * * @param cfgFilePath 配置文件的名 * @return 配置参数Map * @throws Exception */ public static Map<String, String> getConfigParameter(String cfgFilePath) throws Exception { Map<String, String> mcfg = new HashMap<String, String>(); Pattern pp = Pattern.compile(".+properties"); Pattern px = Pattern.compile(".+xml"); Matcher mp = pp.matcher(cfgFilePath); Matcher mx = px.matcher(cfgFilePath); InputStream in = DataSourceConfig.class.getResourceAsStream(cfgFilePath); Properties prop = new Properties(); if (mp.matches()) { prop.load(in); Enumeration<String> enu = (Enumeration<String>) prop.propertyNames(); while (enu.hasMoreElements()) { String key = enu.nextElement(); String value = prop.getProperty(key); mcfg.put(key, value); } in.close(); } else if (mx.matches()) { prop.loadFromXML(in); Enumeration<String> enu = (Enumeration<String>) prop.propertyNames(); while (enu.hasMoreElements()) { String key = enu.nextElement(); String value = prop.getProperty(key); mcfg.put(key, value); } in.close(); } else { throw new Exception("数据源配置文件必须为xml或者properties文件,请检查配置!"); } //检查没有配置的属性,并通过默认的属性值进行设置 Set<Map.Entry<String, String>> def = defcfgmap.entrySet(); for (Map.Entry<String, String> d : def) { if (!mcfg.containsKey(d.getKey())) { mcfg.put(d.getKey(), d.getValue()); } } return mcfg; } /** * 获取默认的数据源配置信息 * * @return */ private static String getDefaultConfig() { return getConfig(defcfgmap); } /** * 返回数据源配置信息的文本表述 * * @param map 配置信息Map * @return 数据源配置信息的文本表述 */ private static String getConfig(Map<String, String> map) { StringBuffer sb = new StringBuffer("数据源配置参数:\n"); for (Map.Entry<String, String> ent : map.entrySet()) { sb.append(ent.getKey()).append("=").append(ent.getValue()).append("\n"); } return sb.toString(); } /** * 测试配置参数获取工具 * * @param args * @throws Exception */ public static void main(String args[]) throws Exception { Map<String, String> mcfg = getConfigParameter("dscfg.xml"); String cfg = getConfig(mcfg); System.out.println(cfg); } } 配置文件
dscfg.properties
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/mysql user=root password=leizhimin dscfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>JDBC数据库连接池实现</comment> <entry key="user">root</entry> <entry key="password">leizhimin</entry> <entry key="url">jdbc:mysql://127.0.0.1:3306/mysql</entry> <entry key="driverClassName">com.mysql.jdbc.Driver</entry> </properties> 现有的方法都测试过了,还没有写导出配置的方法,接下来我会补上。 本文出自 “熔 岩” 博客,转载请与作者联系! 本文出自 51CTO.COM技术博客 |



leizhimin
博客统计信息
热门文章
最新评论
友情链接