原文:http://hi.baidu.com/%B1%CF%C0%F6%C3%F4/blog/item/a1ce3a08cb3990d263d986e3.html
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>
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());
}
}
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;
}
}
}
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");
}
}
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());
}
}