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
    评论
XML解析报错Exception in thread "main" java.lang.NullPointerException通常是由于在解析XML文件时,代码中引用了一个空指针对象而导致的。解决此问题的方法如下: 1.检查XML文件是否正确:检查XML文件是否符合XML规范,是否存在语法错误或标签不匹配等问题。 2.检查代码中的空指针对象:检查代码中是否存在空指针对象,如果存在,则需要对其进行判断或初始化。 3.使用try-catch语句:在解析XML文件时,使用try-catch语句捕获异常,以便更好地处理异常情况。 以下是一个Java解析XML文件的例子,其中包含了try-catch语句来捕获异常: ```java import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.Node; import org.w3c.dom.Element; import java.io.File; public class ReadXMLFile { public static void main(String argv[]) { try { File fXmlFile = new File("file.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(fXmlFile); doc.getDocumentElement().normalize(); System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); NodeList nList = doc.getElementsByTagName("student"); System.out.println("----------------------------"); for (int temp = 0; temp < nList.getLength(); temp++) { Node nNode = nList.item(temp); System.out.println("\nCurrent Element :" + nNode.getNodeName()); if (nNode.getNodeType() == Node.ELEMENT_NODE) { Element eElement = (Element) nNode; System.out.println("Student roll no : " + eElement.getAttribute("rollno")); System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent()); System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent()); System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent()); System.out.println("Marks : " + eElement.getElementsByTagName("marks").item(0).getTextContent()); } } } catch (Exception e) { e.printStackTrace(); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值