Java读取xml文件中oracle数据库连接(sax)(转)

原文:http://hi.baidu.com/%B1%CF%C0%F6%C3%F4/blog/item/a1ce3a08cb3990d263d986e3.html

 

ContractedBlock.gif ExpandedBlockStart.gif DataBaseConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<data>
    
<datasource>
        
<dataname>xf</dataname>
        
<driver>oracle.jdbc.driver.OracleDriver
        
</driver>
        
<url>jdbc:oracle:thin:@localhost:1521:SID
        
</url>
        
<username>xiaofeng</username>
        
<password>xiaofeng</assword>
    
</datasource>
</data>

 

ContractedBlock.gif ExpandedBlockStart.gif ConfigParser.java
import java.util.Properties;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

class ConfigParser extends DefaultHandler {
    
    
private Properties props;    // 定义一个Properties 用来存放属性值
//    private String currentSet;
    private String currentName;
    
private StringBuffer currentValue = new    StringBuffer();

    
// 构建器初始化props
    public ConfigParser() {
        
this.props = new Properties();
    }

    
public Properties getProps() {
        
return this.props;
    }

    
// 定义开始解析元素的方法. 这里是将中的名称xxx提取出来.
    public void startElement(String uri, String    localName, String qName, Attributes attributes) throws SAXException
    {
        currentValue.delete(
0, currentValue.length());
        
this.currentName = qName;
    }

    
// 这里是将之间的值加入到currentValue
    public void characters(char[] ch, int start, int length) throws SAXException {
        currentValue.append(ch, start, length);
    }

    
// 在遇到结束后,将之前的名称和值一一对应保存在props中
    public void endElement(String uri, String localName,String qName) throws SAXException {
        props.put(qName.toLowerCase(),currentValue.toString().trim());
    }
}

  

ContractedBlock.gif ExpandedBlockStart.gif ParseXML.java
import java.util.Properties;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

class ParseXML {

    
private Properties props; // 定义一个Properties 用来存放属性值

    
public Properties getProps() {
        
return this.props;
    }

    
public void parse(String filename) throws Exception {
        
// 将我们的解析器对象化
        ConfigParser handler = new ConfigParser();

        
// 获取SAX工厂对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(
false);
        factory.setValidating(
false);

        
// 获取SAX解析
        SAXParser parser = factory.newSAXParser();

        
try {
            
// 将解析器和解析对象xml联系起来,开始解析
            parser.parse(filename, handler);

            
// 获取解析成功后的属性
            props = handler.getProps();
        } 
finally {
            factory 
= null;
            parser 
= null;
            handler 
= null;
        }
    }
}

 

ContractedBlock.gif ExpandedBlockStart.gif ReadConfigXml.java
import java.util.Properties;

public class ReadConfigXml {
    
private Properties props;

    
public ReadConfigXml(String url) {
        ParseXML myRead 
= new ParseXML();
        
try {
            myRead.parse(url);
            props 
= new Properties();
            props 
= myRead.getProps();
        } 
catch (Exception e) {
            e.printStackTrace();
        }
    }

    
/**
     * getProperty("<标签名>"),与XML文档里标签名相关联 }
     * 另:标签名在这里使用时,统一为小写
     
*/
    
public String getDataName() {
        
return props.getProperty("dataname");
    }

    
public String getDriver() {
        
return props.getProperty("driver");
    }

    
public String getUrl() {
        
return props.getProperty("url");
    }

    
public String getUserName() {
        
return props.getProperty("username");
    }

    
public String getPassWord() {
        
return props.getProperty("password");
    }
}

  

ContractedBlock.gif ExpandedBlockStart.gif DBConnection.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import com.hnii.xml.ReadConfigXml;

public class DBConnection {

    
private Connection con;

    
private DBConnection() {

    }

    
public static DBConnection newInstance() {
        
return new DBConnection();
    }

    
/*
     * public Connection getConnection(){ ReadConfigXml r = new ReadConfigXml
     * 
     * ("mssql.xml"); //读取xml文件中数据库相关信息 String url =
     * 
     * "jdbc:microsoft:sqlserver://"+r.getServerName()
     * 
     * +":"+r.getServerPort()
     * 
     * 
     * +";DatabaseName="+r.getDatabaseName(); String username = r.getUserName();
     * String password = r.getPassWord(); try { Class.forName
     * 
     * ("com.microsoft.jdbc.sqlserver.SQLServerDriver"); con =
     * DriverManager.getConnection(url,
     * 
     * username, password); } catch (ClassNotFoundException e) {
     * e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }
     * return con; }
     * 
     * 
     * //测试连接 public static void main(String args[]){ Connection con =
     * DBConnection.newInstance
     * 
     * ().getConnection(); }
     
*/
    
public static void main(String args[]) {
        ReadConfigXml r 
= new ReadConfigXml("DataBaseConfig.xml"); // xml文件放到工程目录下
        System.out.println(r.getDataName());
        System.out.println(r.getDriver());
        System.out.println(r.getUrl());
        System.out.println(r.getUserName());
        System.out.println(r.getPassWord());

    }
}

 

转载于:https://www.cnblogs.com/myparamita/archive/2009/05/21/1486069.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值