Original article: 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; // defines a property to store the property value.
// Private string currentset;
Private string currentname;
Private stringbuffer currentvalue = new stringbuffer ();
// The Builder initializes props.
Public configparser (){
This. Props = new properties ();
}
Public properties getprops (){
Return this. Props;
}
// Define the method for starting element parsing. Here, the name xxx is extracted.
Public void startelement (string Uri, string localname, string QNAME, attributes) throws saxexception
{
Currentvalue. Delete (0, currentvalue. Length ());
This. currentname = QNAME;
}
// Add the value to currentvalue
Public void characters (char [] CH, int start, int length) throws saxexception {
Currentvalue. append (CH, start, length );
}
// Save the previous names and values in props one by one after the end
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; // defines a property to store the property value.
Public properties getprops (){
Return this. Props;
}
Public void parse (string filename) throws exception {
// Visualize our parser
Configparser handler = new configparser ();
// Obtain the sax factory object
Saxparserfactory factory = saxparserfactory. newinstance ();
Factory. setnamespaceaware (false );
Factory. setvalidating (false );
// Obtain the sax Parsing
Saxparser parser = factory. newsaxparser ();
Try {
// Associate the parser with the parsing object XML to start Parsing
Parser. parse (filename, Handler );
// Obtain the attributes after successful resolution
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 ("<Tag Name>"), associated with the tag name in the XML document}
* In addition, the tag names must be in lowercase.
*/
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"); // read the database information in the XML file 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 ;}
*
*
* // Test the connection public static void main (string ARGs []) {connection con =
* Dbconnection. newinstance
*
* (). Getconnection ();}
*/
Public static void main (string ARGs []) {
Readconfigxml r = new readconfigxml ("databaseconfig. xml"); // put the XML file in the project directory
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 ());
}
}