JDBC数据库连接池的实现:第一天:数据源配置解析工具

本文分享了手动实现JDBC数据库连接池的第一步——数据源配置解析工具的开发过程,支持XML和properties配置文件,无需依赖外部包。
开源的连接池很多,实现都不错,可是就不知道怎么做的,在研读了javax.sql包中的API规范后,想手动实现一个通用的JDBC数据库连接池。这个工作量比较大,主要目的是为了深入底层学习Java处理数据库的技术。
 
第一天:数据源配置解析工具
 
这个解析工具支持xml配置和properties配置,程序不依赖任何jdk以外的任何第三方包。
 
xml和properties文件的格式对名字有限制,xml还对格式有限制,要求符合sun公司的 [url]http://java.sun.com/dtd/properties.dtd[/url]规范。
 
解析工具要完成的功能,读取配置文件,并存放到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([url]http://lavasoft.blog.51cto.com[/url]) 
* 数据源配置工具 
*/
 
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 >
 
 
现有的方法都测试过了,还没有写导出配置的方法,接下来我会补上。


本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/66960,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值