http://www.blogjava.net/bolo/archive/2009/01/04/221068.html
这是原文,里面有一个例子,我下载下来后导入到myeclipse里面去以后出现如下的错误提示,不知是何原因?
XML配置文件未加载成功,请检查
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: org.dom4j.DocumentException: null Nested exception: null
at com.demo.database.DataBaseConnect.load(DataBaseConnect.java:50)
at com.demo.database.DataBaseConnect.<clinit>(DataBaseConnect.java:36)
Caused by: org.dom4j.DocumentException: null Nested exception: null
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.dom4j.io.SAXReader.read(SAXReader.java:343)
at com.demo.database.DataBaseConnect.load(DataBaseConnect.java:46)
... 1 more
这是原文,里面有一个例子,我下载下来后导入到myeclipse里面去以后出现如下的错误提示,不知是何原因?
XML配置文件未加载成功,请检查
java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: org.dom4j.DocumentException: null Nested exception: null
at com.demo.database.DataBaseConnect.load(DataBaseConnect.java:50)
at com.demo.database.DataBaseConnect.<clinit>(DataBaseConnect.java:36)
Caused by: org.dom4j.DocumentException: null Nested exception: null
at org.dom4j.io.SAXReader.read(SAXReader.java:484)
at org.dom4j.io.SAXReader.read(SAXReader.java:343)
at com.demo.database.DataBaseConnect.load(DataBaseConnect.java:46)
... 1 more
解决方案 »
- 关于getResourceAsStream方法抛java.net.MalformedURLException的问题 高手请进 急~~~
- 在PLSQL Developer环境下怎样 延长与oracle的 会话时间
- 此课题为了解决软件开发中的业务更改繁琐的问题(java)
- xml转换后
- EJB的缺点
- spring2.0 struts2.0 hibernate3.1 extjs组合登陆问题
- struts难道有bug吗?不知大家有没有碰到过这样的问题!
- Tomcat 下面的第一个 Servlet
- 请问struts里由action转发到的jsp页面里可以使用两个form吗?
- BorderLayout布局 不自动排列?
- 出错了!!
- NetBeans 关于GUI设计的问题
把这个类的代码贴出来看看
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;import com.demo.util.ClassUtils;
import com.demo.util.Constant;/**
* 数据库连接管理
* @author 菠萝大象
* @version 1.0
*/
public class DataBaseConnect {
private static Element coreModel; //根元素
private static String driver; //连接驱动
private static String url; //数据库连接地址
private static String username; //用户名
private static String password; //密码
/**
* 静态初始化
*/
static{
load();
}
/**
* 加载数据源配置文件,取得根元素
*/
private static void load(){
SAXReader saxReader = new SAXReader();
InputStream in = ClassUtils.getResourceAsStream(Constant.DB_CONFIG); //加载数据库XML配置文件
try{
Document document = saxReader.read(in); //得到Document对象
coreModel = document.getRootElement(); //得到根元素
}catch(Exception e){
System.out.println("XML配置文件未加载成功,请检查");
throw new RuntimeException(e);
}
}
/**
* 构造函数初始化
*/
public DataBaseConnect(){
HashMap<String,String> hashmap = new HashMap<String,String>();
hashmap = iterateXML(hashmap);//取得数据源配置信息
driver = hashmap.get(Constant.DRIVER);
url = hashmap.get(Constant.URL);
username = hashmap.get(Constant.USERNAME);
password = hashmap.get(Constant.PASSWORD);
}
/**
* 取得数据源配置信息放入HashMap中
* @param hashmap 存放取得的数据源键值对
* @return 返回存放信息的HashMap
*/
public static HashMap<String,String> iterateXML(HashMap<String,String> hashmap){
try{
Element database = getElement("//demo/database"); //得到database节点
List list = database.elements(); //得到database元素下的子元素集合
/*
* 循环遍历集合中的每一个元素
* 将每一个元素的元素名和值作为键值对放入HashMap中
*/
for(Object obj:list){
Element element = (Element)obj;
//getName()是元素名,getText()是元素值
hashmap.put(element.getName(), element.getText());
}
}catch(Exception e){
e.printStackTrace();
}
return hashmap;
}
/**
* 通过XPATH得到元素对象
* @param xPath XPATH路径
* @return 获得Element对象
*/
public static Element getElement(String xPath){
return (Element)coreModel.selectSingleNode(xPath);
}
/**
* 获得数据库连接
*/
public Connection getConnection(){
Connection con = null;
try{
Class.forName(driver);
con =DriverManager.getConnection(url, username, password);
}catch(Exception e){
throw new RuntimeException(e);
}
return con;
}
/**
* 关闭数据连接
* @param con 要关闭的数据库连接
*/
public void close(Connection con){
try {
if (con != null && !con.isClosed()) {
con.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally{
try{
if(con != null && !con.isClosed()){
con.close();
}
}catch(Exception e){
throw new RuntimeException(e);
}
}
}
public static void main(String[] args){
System.out.println(username);
/* DataBaseConnect db = new DataBaseConnect();
Connection con = db.getConnection();
try{
PreparedStatement psmt = con.prepareStatement("select count(*) from help_keyword");
ResultSet rs = psmt.executeQuery();
rs.next();
System.out.println(rs.getString(1));
rs.close();
psmt.close();
db.close(con);
}catch(Exception e){
throw new RuntimeException(e);
}
*/
}
}
这样的东西 读两行就搞不下去了,眼睛花阿
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;import com.demo.util.ClassUtils;
import com.demo.util.Constant;/**
* 数据库连接管理
* @author 菠萝大象
* @version 1.0
*/
public class DataBaseConnect {
private static Element coreModel; //根元素
private static String driver; //连接驱动
private static String url; //数据库连接地址
private static String username; //用户名
private static String password; //密码
/**
* 静态初始化
*/
static{
load();
}
/**
* 加载数据源配置文件,取得根元素
*/
private static void load(){
SAXReader saxReader = new SAXReader();
InputStream in = ClassUtils.getResourceAsStream(Constant.DB_CONFIG); //加载数据库XML配置文件
try{
Document document = saxReader.read(in); //得到Document对象
coreModel = document.getRootElement(); //得到根元素
}catch(Exception e){
System.out.println("XML配置文件未加载成功,请检查");
throw new RuntimeException(e);
}
}
/**
* 构造函数初始化
*/
public DataBaseConnect(){
HashMap<String,String> hashmap = new HashMap<String,String>();
hashmap = iterateXML(hashmap);//取得数据源配置信息
driver = hashmap.get(Constant.DRIVER);
url = hashmap.get(Constant.URL);
username = hashmap.get(Constant.USERNAME);
password = hashmap.get(Constant.PASSWORD);
}
/**
* 取得数据源配置信息放入HashMap中
* @param hashmap 存放取得的数据源键值对
* @return 返回存放信息的HashMap
*/
public static HashMap<String,String> iterateXML(HashMap<String,String> hashmap){
try{
Element database = getElement("//demo/database"); //得到database节点
List list = database.elements(); //得到database元素下的子元素集合
/*
* 循环遍历集合中的每一个元素
* 将每一个元素的元素名和值作为键值对放入HashMap中
*/
for(Object obj:list){
Element element = (Element)obj;
//getName()是元素名,getText()是元素值
hashmap.put(element.getName(), element.getText());
}
}catch(Exception e){
e.printStackTrace();
}
return hashmap;
}
/**
* 通过XPATH得到元素对象
* @param xPath XPATH路径
* @return 获得Element对象
*/
public static Element getElement(String xPath){
return (Element)coreModel.selectSingleNode(xPath);
}
/**
* 获得数据库连接
*/
public Connection getConnection(){
Connection con = null;
try{
Class.forName(driver);
con =DriverManager.getConnection(url, username, password);
}catch(Exception e){
throw new RuntimeException(e);
}
return con;
}
/**
* 关闭数据连接
* @param con 要关闭的数据库连接
*/
public void close(Connection con){
try {
if (con != null && !con.isClosed()) {
con.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally{
try{
if(con != null && !con.isClosed()){
con.close();
}
}catch(Exception e){
throw new RuntimeException(e);
}
}
}
public static void main(String[] args){
System.out.println(username);
/* DataBaseConnect db = new DataBaseConnect();
Connection con = db.getConnection();
try{
PreparedStatement psmt = con.prepareStatement("select count(*) from help_keyword");
ResultSet rs = psmt.executeQuery();
rs.next();
System.out.println(rs.getString(1));
rs.close();
psmt.close();
db.close(con);
}catch(Exception e){
throw new RuntimeException(e);
}
*/
}
}
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;import com.demo.util.ClassUtils;
import com.demo.util.Constant;/**
* 数据库连接管理
* @author 菠萝大象
* @version 1.0
*/
public class DataBaseConnect {private static Element coreModel; //根元素
private static String driver; //连接驱动
private static String url; //数据库连接地址
private static String username; //用户名
private static String password; //密码/**
* 静态初始化
*/
static{
load();
}/**
* 加载数据源配置文件,取得根元素
*/
private static void load(){
SAXReader saxReader = new SAXReader();
InputStream in = ClassUtils.getResourceAsStream(Constant.DB_CONFIG); //加载数据库XML配置文件
try{
Document document = saxReader.read(in); //得到Document对象
coreModel = document.getRootElement(); //得到根元素
}catch(Exception e){
System.out.println("XML配置文件未加载成功,请检查");
throw new RuntimeException(e);
}
}/**
* 构造函数初始化
*/
public DataBaseConnect(){
HashMap<String,String> hashmap = new HashMap<String,String>();
hashmap = iterateXML(hashmap);//取得数据源配置信息
driver = hashmap.get(Constant.DRIVER);
url = hashmap.get(Constant.URL);
username = hashmap.get(Constant.USERNAME);
password = hashmap.get(Constant.PASSWORD);
}/**
* 取得数据源配置信息放入HashMap中
* @param hashmap 存放取得的数据源键值对
* @return 返回存放信息的HashMap
*/
public static HashMap<String,String> iterateXML(HashMap<String,String> hashmap){
try{
Element database = getElement("//demo/database"); //得到database节点
List list = database.elements(); //得到database元素下的子元素集合
/*
* 循环遍历集合中的每一个元素
* 将每一个元素的元素名和值作为键值对放入HashMap中
*/
for(Object obj:list){
Element element = (Element)obj;
//getName()是元素名,getText()是元素值
hashmap.put(element.getName(), element.getText());
}
}catch(Exception e){
e.printStackTrace();
}
return hashmap;
}/**
* 通过XPATH得到元素对象
* @param xPath XPATH路径
* @return 获得Element对象
*/
public static Element getElement(String xPath){
return (Element)coreModel.selectSingleNode(xPath);
}/**
* 获得数据库连接
*/
public Connection getConnection(){
Connection con = null;
try{
Class.forName(driver);
con =DriverManager.getConnection(url, username, password);
}catch(Exception e){
throw new RuntimeException(e);
}
return con;
}/**
* 关闭数据连接
* @param con 要关闭的数据库连接
*/
public void close(Connection con){
try {
if (con != null && !con.isClosed()) {
con.close();
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally{
try{
if(con != null && !con.isClosed()){
con.close();
}
}catch(Exception e){
throw new RuntimeException(e);
}
}
}public static void main(String[] args){
System.out.println(username);
/* DataBaseConnect db = new DataBaseConnect();
Connection con = db.getConnection();
try{
PreparedStatement psmt = con.prepareStatement("select count(*) from help_keyword");
ResultSet rs = psmt.executeQuery();
rs.next();
System.out.println(rs.getString(1));
rs.close();
psmt.close();
db.close(con);
}catch(Exception e){
throw new RuntimeException(e);
}
*/
}}