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

使用Jsoup和Dom4j封装jdbc连接数据库
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import org.apache.log4j.PropertyConfigurator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jsoup.Jsoup;

import com.app.entity.sys.JdbcInfo;

public class PropertyUtil {
	private Document doc;
	private JdbcInfo jdbcInfo; 
	public JdbcInfo getJdbcInfo() {
		if(jdbcInfo==null)
		jdbcInfo=new JdbcInfo();
		return jdbcInfo;

	}
	/**
	 * ==============================================================
	 */

	private PropertyUtil() {
		try {
			doc = new SAXReader().read(Thread.currentThread().getContextClassLoader().getResourceAsStream("sys-config.xml"));
			Element driverNameElement = (Element) doc.selectObject("/config/db-info/driver-name");
			Element urlElement = (Element) doc.selectObject("/config/db-info/url");
			Element usernameElement = (Element) doc.selectObject("/config/db-info/username");
			Element passwordElement = (Element) doc.selectObject("/config/db-info/password");
			jdbcInfo = new JdbcInfo();
			jdbcInfo.setDriverName(driverNameElement.getStringValue());//获取driver-name节点值
			jdbcInfo.setUrl(urlElement.getStringValue());
			jdbcInfo.setUsername(usernameElement.getStringValue());
			jdbcInfo.setPassword(passwordElement.getStringValue());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 利用Jsoup解析xml
	 * @throws Exception
	 */
	private static void Jsoup()throws Exception{
		String pathname="src/config/doc/xml/sys-config.xml";
		File flie=new File(pathname);
		org.jsoup.nodes.Document doc = Jsoup.parse(flie, "UTF-8");
		String driverName=doc.select("driver-name").text();//获取driver-name节点值
		String url=doc.select("url").text();
		String username=doc.select("username").text();
		String password=doc.select("password").text();
		System.out.println(password);
	} 
}

?



package com.app.entity.sys;

import java.io.Serializable;

public class JdbcInfo  implements Serializable {
	private String driverName;
	private String url;
	private String username;
	private String password;

	public String getDriverName() {
		return driverName;
	}

	public JdbcInfo(String driverName, String url, String username,
			String password) {
		this.driverName=driverName;
		this.url=url;
		this.username=username;
		this.password=password;

	}

	public JdbcInfo() {
	}

	@Override
	public String toString() {
		return "JdbcInfo [driverName=" + driverName + ", password=" + password
				+ ", url=" + url + ", username=" + username + "]";
	}

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

?

<?xml version="1.0" encoding="UTF-8"?> 
<config> 
	<db-info> 
		<driver-name>oracle.jdbc.driver.OracleDriver</driver-name> 
		<url>jdbc:oracle:thin:@localhost:1521:oracle</url> 
		<username>sa</username> 
		<password>sa</password> 
	</db-info> 
</config> 

?