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;
	}
}

你可能感兴趣的:(xml,jdbc,String,url,Class,encoding)