我按照《J2EE应用开发详解》一书中的源码进行测试SAX的编程,使用XML文件连接mysql数据库,可是在测试时报错,后来发现其中的URL值为空,请问是什么原因啊?谢谢!源码如下:
database.conf.xml文件:
<database-conf>
<datasource>
<driver>org.gjt.mm.mysql.Driver</driver>
<url>jdbc:mysql://127.0.0.1/j2ee14</url>
<user>bn</user>
<password>bn</password>
</datasource>
</database-conf>ConfigParser.java文件(为解析XML,编写的Handler):package com.j2ee14.ch4;
import  org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;import java.util.Properties;/**
 *ConfigParser扩展了DefaultHandler,它用于获得数据库的连接属性
 */
public class ConfigParser extends DefaultHandler
{

private Properties props;
private StringBuffer currentValue =new StringBuffer();
private String currentName;
public ConfigParser()
{
this.props=new Properties();
}
public Properties getProps()
{
return this.props;
}

public void startElement(String uri,String localName,String qName,Attributes attributes)throws SAXException
{
currentValue.delete(0,currentValue.length());
this.currentName=qName;
}
public void characters(char[] ch, int start, int length) throws SAXException {  currentValue.append(ch, start, length); }    /**
     *把XML配置文件的中相关的属性保存到Properties对象中
     */
public void endElement(String uri,String localName,String qName)throws SAXException
{
props.put(qName.toLowerCase(),currentValue.toString().trim());
}
}
ParseDatabaseConfig.java文件(解析XML的类):package com.j2ee14.ch4;import java.util.Properties;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;import java.net.URL;/**
 *其它程序通过ParseDatabaseConfig来获得数据库的配置信息,
 *这样使得类之间的耦合松散
 */
public class ParseDatabaseConfig
{
private Properties props;
public Properties getProps()
{
return this.props;
}
/**
 *解析XML配置文件,把属性保存起来
 */
public void parse(String filename)throws Exception
{
ConfigParser handler=new ConfigParser();
SAXParserFactory factory=SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
factory.setValidating(false);
SAXParser parser=factory.newSAXParser();

URL confURL=ConfigParser.class.getClassLoader().getResource(filename);

try
{
parser.parse(confURL.toString(),handler);
props=handler.getProps();
}
finally
{
factory=null;
parser=null;
handler=null;
}
}}
MyDatabaseApplication.java文件(连接数据库应用类):package com.j2ee14.ch4;import java.util.Properties;
import java.sql.*;/**
 *MyDatabaseApplication是一个示例应用,它通过ParseDatabaseConfig
 *来获得数据库的连接属性以连接数据库
 */
public class MyDatabaseApplication
{
private Connection con;
Properties dbProps;
/**
 *构造方法,获得数据库的连接属性
 */
public MyDatabaseApplication()throws Exception
{
ParseDatabaseConfig databaseConfig=new ParseDatabaseConfig();
databaseConfig.parse("database.conf.xml");
this.dbProps=databaseConfig.getProps();
}
public Connection getConnection()throws java.sql.SQLException
{
try
{
Class.forName(dbProps.getProperty("driver"));

}
catch(java.lang.ClassNotFoundException e)
{
System.err.println("Not Found Driver:"+dbProps.getProperty("driver"));
}
//使用配置的属性创建一个连接。
return java.sql.DriverManager.getConnection(
      dbProps.getProperty("url"),
      dbProps.getProperty("user"),
      dbProps.getProperty("password"));
   }
   
   /**
    *测试方法
    */
   public void doBusiness()
   {
   
     try
     {
       con=getConnection();
      Statement stmt=con.createStatement();
      System.out.println("创建一个表...");
      stmt.execute("create table testconfig(id int not null,name varchar(20),primary key(id))");
       System.out.println("在表中添加数据...");
      stmt.execute("insert into testconfig values('001','hellking')");
      ResultSet rst=stmt.executeQuery("select * from testconfig");
      System.out.println("读取表中的数据...");
      while(rst.next())
      {
       System.out.println("id:"+rst.getInt("id"));
       System.out.println("name:"+rst.getString("name"));
      }
      rst.close();
      stmt.execute("drop table testconfig");
      con.close();
   }
   catch(java.sql.SQLException se)
   {
      System.err.println("连接数据库或者操作发生错误");
      se.printStackTrace(System.err);
   }
     
   finally
   {
      try
      {
       con.close();
      }
      catch(Exception e){}
   }
   }
  
   public static void main(String[] args)
   {
   System.out.println("使用XML作为数据库的配置。\n");   try
   {
   MyDatabaseApplication app=new MyDatabaseApplication();
   app.doBusiness();
   }
   catch(Exception e)
   {
   System.err.println("发生异常");
   }
   }
  
  
}

解决方案 »

  1.   

    自己顶一下,感觉是ParseDatabaseConfig.java类中
    URL confURL=ConfigParser.class.getClassLoader().getResource(filename);
    的问题,因为测试时该值为空。
      

  2.   

    ParseDatabaseConfig.java文件(解析XML的类):package com.j2ee14.ch4;import java.util.Properties;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;import java.net.URL;/**
     *其它程序通过ParseDatabaseConfig来获得数据库的配置信息,
     *这样使得类之间的耦合松散
     */
    public class ParseDatabaseConfig
    {
    private Properties props;
    public Properties getProps()
    {
    return this.props;
    }
    /**
     *解析XML配置文件,把属性保存起来
     */
    public void parse(String filename)throws Exception
    {
    ConfigParser handler=new ConfigParser();
    SAXParserFactory factory=SAXParserFactory.newInstance();
    factory.setNamespaceAware(false);
    factory.setValidating(false);
    SAXParser parser=factory.newSAXParser();

    try
    {
    parser.parse(filename,handler);
    props=handler.getProps();
    }
    finally
    {
    factory=null;
    parser=null;
    handler=null;
    }
    }}
    就可以了,来者给分,揭帖。