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

//------------DataBaseConfig.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <data>  
  3.  <datasource>  
  4.   <dataname>xf</dataname>  
  5.   <driver>oracle.jdbc.driver.OracleDriver</driver>  
  6.   <url>jdbc:oracle:thin:@localhost:1521:SID</url>  
  7.   <username>xiaofeng</username>  
  8.   <password>xiaofeng</password>  
  9.  </datasource>  
  10. </data>  

 

//---------------------ConfigParser.java

java 代码
  1. package zwf.xml;   
  2. import java.util.Properties;   
  3. import org.xml.sax.Attributes;   
  4. import org.xml.sax.SAXException;   
  5. import org.xml.sax.helpers.DefaultHandler;   
  6.   
  7. class ConfigParser extends DefaultHandler {   
  8. //定义一个Properties 用来存放属性值   
  9. private Properties props;   
  10. private String currentSet;   
  11. private String currentName;   
  12. private StringBuffer currentValue = new StringBuffer();   
  13.   
  14. // 构建器初始化props   
  15. public ConfigParser() {   
  16.          this.props = new Properties();   
  17. }   
  18.   
  19. public Properties getProps() {   
  20.          return this.props;   
  21. }   
  22.   
  23. // 定义开始解析元素的方法. 这里是将中的名称xxx提取出来.   
  24. public void startElement(String uri, String localName, String qName,   
  25.           Attributes attributes) throws SAXException {   
  26.          currentValue.delete(0, currentValue.length());   
  27.          this.currentName = qName;   
  28. }   
  29.   
  30. // 这里是将之间的值加入到currentValue   
  31. public void characters(char[] ch, int start, int length)   
  32.           throws SAXException {   
  33.          currentValue.append(ch, start, length);   
  34. }   
  35.   
  36. // 在遇到结束后,将之前的名称和值一一对应保存在props中   
  37. public void endElement(String uri, String localName, String qName)   
  38.           throws SAXException {   
  39.          props.put(qName.toLowerCase(), currentValue.toString().trim());   
  40. }   
  41.   
  42. }   
  43.   

//-----------------ParseXML.java

java 代码
  1. package zwf.xml;   
  2.   
  3. import java.util.Properties;   
  4. import javax.xml.parsers.SAXParser;   
  5. import javax.xml.parsers.SAXParserFactory;   
  6.   
  7. class ParseXML {   
  8. // 定义一个Properties 用来存放属性值   
  9. private Properties props;   
  10.   
  11. public Properties getProps() {   
  12.          return this.props;   
  13. }   
  14.   
  15. public void parse(String filename) throws Exception {   
  16.          // 将我们的解析器对象化   
  17.          ConfigParser handler = new ConfigParser();   
  18.          // 获取SAX工厂对象   
  19.          SAXParserFactory factory = SAXParserFactory.newInstance();   
  20.          factory.setNamespaceAware(false);   
  21.          factory.setValidating(false);   
  22.          // 获取SAX解析   
  23.          SAXParser parser = factory.newSAXParser();   
  24.          try {   
  25.           // 将解析器和解析对象xml联系起来,开始解析   
  26.           parser.parse(filename, handler);   
  27.           // 获取解析成功后的属性   
  28.           props = handler.getProps();   
  29.          } finally {   
  30.           factory = null;   
  31.           parser = null;   
  32.           handler = null;   
  33.          }   
  34. }   
  35. }   
  36.   

//-----------------ReadConfigXml.java

java 代码
  1. package zwf.xml;   
  2.   
  3.   
  4. import java.util.Properties;   
  5.   
  6. public class ReadConfigXml {   
  7. private Properties props;   
  8.   
  9. public ReadConfigXml(String url) {   
  10.          ParseXML myRead = new ParseXML();   
  11.          try {   
  12.           myRead.parse(url);   
  13.           props = new Properties();   
  14.           props = myRead.getProps();   
  15.          } catch (Exception e) {   
  16.           e.printStackTrace();   
  17.          }   
  18. }   
  19. /*  
  20. public String getServerName() {  
  21.          return props.getProperty("servername");   //getProperty("<标签名>"),与XML文档里标签名相关联  
  22. }                                                   //另:标签名在这里使用时,统一为小写  
  23.  
  24. public String getServerPort(){  
  25.          return props.getProperty("serverport");   //getProperty("<标签名>"),与XML文档里标签名相关联  
  26. }  
  27.  
  28. public String getDatabaseName() {  
  29.          return props.getProperty("databasename"); //getProperty("<标签名>"),与XML文档里标签名相关联  
  30. }  
  31.  
  32. public String getUserName() {  
  33.          return props.getProperty("username");     //getProperty("<标签名>"),与XML文档里标签名相关联  
  34. }  
  35.  
  36. public String getPassWord() {  
  37.          return props.getProperty("password");     //getProperty("<标签名>"),与XML文档里标签名相关联  
  38. }  
  39.  
  40. */  
  41.   
  42. public String getDataName(){   
  43.  return props.getProperty("dataname");   
  44. }   
  45. public String getDriver(){   
  46.  return props.getProperty("driver");   
  47. }   
  48. public String getUrl(){   
  49.  return props.getProperty("url");   
  50. }   
  51. public String getUserName(){   
  52.  return props.getProperty("username");   
  53. }   
  54. public String getPassWord(){   
  55.  return props.getProperty("password");   
  56. }   
  57. }   

//---------------------DBConnection.java

java 代码

 

  1. package zwf.xml;   
  2.   
  3. import java.sql.Connection;   
  4. import java.sql.DriverManager;   
  5. import java.sql.SQLException;   
  6.   
  7. import zwf.xml.ReadConfigXml;   
  8.   
  9. public class DBConnection {   
  10.   
  11. private Connection con;   
  12.   
  13. private DBConnection() {   
  14.      
  15. }   
  16.   
  17. public static DBConnection newInstance(){   
  18.          return new DBConnection();   
  19. }   
  20. /*  
  21. public Connection getConnection(){  
  22.          ReadConfigXml r = new ReadConfigXml("mssql.xml");    //读取xml文件中数据库相关信息  
  23.          String url = "jdbc:microsoft:sqlserver://"+r.getServerName()+":"+r.getServerPort()  
  24.                             +";DatabaseName="+r.getDatabaseName();  
  25.          String username = r.getUserName();  
  26.          String password = r.getPassWord();  
  27.          try {  
  28.           Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");  
  29.           con = DriverManager.getConnection(url, username, password);  
  30.          } catch (ClassNotFoundException e) {  
  31.           e.printStackTrace();  
  32.          } catch (SQLException e) {  
  33.           e.printStackTrace();  
  34.          }  
  35.          return con;  
  36. }  
  37.  
  38.  
  39.          //测试连接  
  40.          public static void main(String args[]){  
  41.           Connection con = DBConnection.newInstance().getConnection();  
  42.          }  
  43. */  
  44. public static void main(String args[]){   
  45.  ReadConfigXml r = new ReadConfigXml("DataBaseConfig.xml");  //xml文件放到工程目录下   
  46.  System.out.println(r.getDataName());   
  47.  System.out.println(r.getDriver());   
  48.  System.out.println(r.getUrl());   
  49.  System.out.println(r.getUserName());   
  50.  System.out.println(r.getPassWord());   
  51.   
  52.     
  53. }   
  54. }   
评论
hlydlp 2008-05-29   回复
这个能读取多条数据吗?
我看了只能读取单个配置的。
如果能读取多条配置就好了,这样就可以处理一个一对多的处理。
发表评论

您还没有登录,请登录后发表评论