日期:2014-05-16  浏览次数:20529 次

使用XML配置c3p0数据库连接池

想通过JDBC来配置c3p0数据库连接池,上网想找到解析工具没有找到,只要自己写了一个,可以读取在根目录下的
c3p0配置文档,再根据配置信息来反射ComboPooledDataSource ,中的setter,进行连接池的配置。
本人水平很菜,如果有什么不足请大家指出。

?

package cn.vagasnail.sgms.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 得到c3p0的数据库连接池
 * 
 * @author vagasnail
 * 
 * 2009-3-26 下午07:54:10
 */
public class C3p0ConnectionFactory {
	private C3p0ConnectionFactory() {
	}

	private static ComboPooledDataSource ds = null;
	static {
		Map<String, String> configs = C3p0XMLParser
				.parserXML("c3p0-config.xml"); // 得到一个map包含配置信息
		ds = new ComboPooledDataSource(); // 得到一个数据库连接池数据源
		Method[] fs = ComboPooledDataSource.class.getMethods(); // 得到该数据库连接池数据源的所以方法信息
		Pattern pattern = Pattern.compile("get([A-Z]\\w+)()");
		for (Method m : fs) {
			Matcher matcher = pattern.matcher(m.getName());
			String str = "";
			String strLow = "";
			if (matcher.find()) {
				str = matcher.group(1);
				char c = str.charAt(0);
				strLow = Character.toLowerCase(c) + str.substring(1);
			}

			try {
				String value = configs.get(strLow);
				int intValue = 0;
				Method setMod  = null;
				if (value != null && !"".equals(value)){
				try{
					intValue = Integer.parseInt(value);
					 setMod = ComboPooledDataSource.class.getMethod("set"
							+ str, int.class);  //如果反射的方法中是int类型,而不是包装类Interger,那么无法反射到该犯法,反射调用方法无法自动解包
                       if(setMod != null)
						setMod.invoke(ds, intValue);
                       
		
				}catch(NumberFormatException nfe){
					setMod = ComboPooledDataSource.class.getMethod("set"
							+ str, String.class);
					if(setMod != null)
					setMod.invoke(ds, value);
				}
				}else
					continue;
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				// e.printStackTrace(); //如果方法不存在则继续下面的循环
				continue;
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			
	
			
		}
		
		login(configs);
	}
	
	private static void login(Map<String, String> configs){	
		ds.setUser(configs.get("user"));
		ds.setPassword(configs.get("password"));
		
	}

	/**
	 * 得到一个数据库连接
	 * 
	 * @return 数据库连接
	 */
	public static synchronized Connection getConnection() {

		Connection con = null;
		try {
			con = ds.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		return con;
	}
/**
 * 关闭数据库(并没有真正的关闭,连接池把连接存储起来下一次调用的时候还可以使用)
 * @param conn
 * @throws SQLException 
 *//*
	public static synchronized void close(Connection conn) {
		if (conn != null) {
			try {
				conn.close();
				conn = null;
			} catch (SQLException e) {
				e.printStackTrace();
				return;
			}
		}

	}*/
	
	public static int getCurrentConns() throws SQLException{
		return ds.getNumConnectionsAllUsers();
	}

}

?

1 楼 stevensinclair 2009-03-31  
啥对啥?!
2 楼 daiming253685 2009-04-01  
stevensinclair 写道

啥对啥?!

我还不是很会使用javaeye提供的功能