java的dbfactory,DBFactory.java

package com.idbk.common.dal;

import java.io.FileNotFoundException;

import java.io.InputStream;

import java.sql.*;

import java.util.Properties;

import javax.sql.*;

import org.apache.log4j.Logger;

import com.idbk.common.util.DisposeUtil;

import java.lang.reflect.*;

public class DBFactory {

public enum DatabaseTypeEnum {

MySQL, Oracle

}

private static final Logger logger = Logger.getLogger(DBFactory.class);

private static DataSource ds = null;

private static DatabaseTypeEnum databaseType;

private static String fixMethodName(String methodName){

String result = methodName.replace("set", "");

String str1 = "" + result.charAt(0);

String str2 = result.substring(1);

return str1.toLowerCase() + str2;

}

private static DataSource getDataSource(Properties ps) throws Exception {

String dsClass = "org.apache.commons.dbcp.BasicDataSource";

if(ps.getProperty("dataSourceClass") != null)

dsClass = ps.getProperty("dataSourceClass");

Class extends Object> cls = Class.forName(dsClass);

logger.debug("DataSource " + dsClass);

DataSource ds = (DataSource)cls.newInstance();

Method[] methods = cls.getDeclaredMethods();

for(Method method : methods){

if(Modifier.isPublic(method.getModifiers()) == false)

continue;

String name = method.getName();

if(name.startsWith("set") == false)

continue;

name = fixMethodName(name);

String strValue = ps.getProperty(name);

if(strValue == null)

continue;

strValue = strValue.trim();

Class extends Object>[] paramClses = method.getParameterTypes();

if(paramClses.length != 1)

continue;

Class extends Object> paramCls = paramClses[0];

if(paramCls.getName().equals("int")){

method.invoke(ds, Integer.parseInt(strValue));

logger.debug("Set " + name);

}else if(paramCls.getName().equals("java.lang.String")){

method.invoke(ds, strValue);

logger.debug("Set " + name);

}

}

return ds;

}

public static boolean init(){

InputStream in = null;

try {

in = (DBFactory.class.getClassLoader()

.getResourceAsStream("jdbc.properties"));

if(in == null){

in = (DBFactory.class.getClassLoader()

.getResourceAsStream("config.properties"));

}

if (in == null) {

throw new FileNotFoundException("配置文件jdbc.properties未找到");

}

Properties props = new Properties();

props.load(in);

in.close();

ds = getDataSource(props);

String database = props.getProperty("database");

if("mysql".equals(database))

databaseType = DatabaseTypeEnum.MySQL;

else if("oracle".equals(database))

databaseType = DatabaseTypeEnum.Oracle;

else

throw new Exception("Property 'database' invalid or not defined.");

return true;

} catch (Exception e) {

logger.error(new java.util.Date() + " : " + e.getMessage(), e);

return false;

}finally{

DisposeUtil.safeClose(in);

}

}

static {

init();

}

public static void setDataSource(DataSource value){

ds = value;

}

public static DatabaseTypeEnum getDatabaseType(){

return databaseType;

}

public static Connection newConnection() throws SQLException {

return ds.getConnection();

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值