XML配置文件的读取--DOM4J

    这里使用的Dom4j的功能包。

代码如下:

1、xml配置文件 

<?xml version="1.0" encoding="UTF-8"?>
<config>
	<db-info>
		<driver-name>com.mysql.jdbc.Driver</driver-name>
		<url>jdbc:mysql://localhost/yingshe</url>
		<user-name></user-name>
		<password></password>
	</db-info>
</config>


其位于所写功能包的最上层

2、java代码

package com.bjpowernode.drp.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 封装数据常用操作
 * @author Administrator
 */
public class DbUtil {
	public static Connection getConnection(){
		Connection conn = null;
		JdbcConnConfig config = XmlConfigReader.getInstance().getJdbcConnConfig();

		String driverName = config.getDriverName();
		String url = config.getUrl();
		String userName = config.getUserName();
		String password = config.getPassword();
		
		try {
			Class.forName(driverName);
			conn = DriverManager.getConnection(url, userName, password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}		

		return conn;
	}
}


 

package com.bjpowernode.drp.util;

public class JdbcConnConfig {
	private String driverName ;
	private String url ;
	private String userName ;
	private String password ;

	public String getDriverName() {
		return driverName;
	}
	public void setDriverName(String driverName) {
		this.driverName = driverName;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "[ driverName:" + driverName + ", url:" + url + ", userName:" + userName + " ]";
	}
}

package com.bjpowernode.drp.util;

import java.io.InputStream;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class XmlConfigReader {
	//延时加载
	private static XmlConfigReader instance = null;
	
	private JdbcConnConfig conn = new JdbcConnConfig();
	
	private XmlConfigReader(){
		SAXReader reader = new SAXReader();
		//装载XML文件--按资源加载的方式加载
		InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("sys-config.xml");
		try {
			//把输入流的文件存入文档对象中
			Document doc = reader.read(in);
			Node node = (Node) doc.selectObject("/config/db-info");
			String driverName = node.valueOf("driver-name");
			String url = node.valueOf("url");
			String userName = node.valueOf("user-name");
			String password = node.valueOf("password");
			
			conn.setDriverName(driverName);
			conn.setUrl(url);
			conn.setUserName(userName);
			conn.setPassword(password);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	public  static synchronized XmlConfigReader getInstance(){
		if(instance == null){
			instance = new XmlConfigReader();
		}
		return instance;
	}

	/**
	 * 返回JDBC连接配置
	 */
	public JdbcConnConfig getJdbcConnConfig(){
		return conn;
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

收获de季节

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值