Here we have three classes
ConfigParser.class
import java.util.Properties;
import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
public class ConfigParser extends DefaultHandler {
private Properties props;
private String currentName;
private StringBuffer currentValue = new StringBuffer();
public ConfigParser() {
this.props = new Properties();
}
public Properties getProps() {
return this.props;
}
public void startElement(String aUri, String aLocalName, String aQName,
Attributes attributes) throws SAXException {
currentValue.delete(0, currentValue.length());
this.currentName = aQName;
}
public void characters(char[] ch, int start, int length)
throws SAXException {
currentValue.append(ch, start, length);
}
/**
* Description:Save database.conf.xml to Properties <br>
* Input: <br>
* Output: result = all the result <br>
* Return: ResultSet result <br>
* Remark: <br>
*
*/
public void endElement(String aUri, String aLocalName, String aQName)
throws SAXException {
props.put(aQName.toLowerCase(), currentValue.toString().trim());
}
}
//
/**
* @Program Name: ParseDatabaseConfig<br>
* @Description: Through this class to get database config information <br>
*/
package com.eissha.mis;
import java.net.URL;
import java.util.Properties;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class ParseDatabaseConfig {
private Properties props;
public Properties getProps() {
return this.props;
}
/**
* Description:read XML file and save properties <br>
* Input: String filename<br>
* Output: none <br>
* Return: none <br>
* Remark: <br>
*
*/
public void parse(String aFileName) 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(aFileName);
try {
parser.parse(confURL.toString(), handler);
props = handler.getProps();
} finally {
factory = null;
parser = null;
handler = null;
}
}
}
/**
* @Program Name: ConnectDatabase<br>
* @Description: Through ParserDatabaseConfig to get connection to database<br>
*
*/
package com.eissha.mis;
import java.sql.Connection;
import java.util.Properties;
public class ConnectDatabase {
Properties dbProps;
/**
* Description:Constructor get properties of dabase <br>
* Input: none <br>
* Output: none <br>
* Return: none <br>
* Remark: <br>
*
*/
public ConnectDatabase() throws Exception {
ParseDatabaseConfig dbConfig = new ParseDatabaseConfig();
dbConfig.parse("com/eissha/mis/database.conf.xml");
this.dbProps = dbConfig.getProps();
}
/**
* Description:method of getting connection to dabase <br>
* Input: none <br>
* Output: coon <br>
* Return: Connection conn <br>
* Remark: <br>
*
*/
public Connection getConnection() throws java.sql.SQLException {
try {
Class.forName(dbProps.getProperty("driver"));
} catch (ClassNotFoundException e) {
System.out.println("Not Found Driver:"
+ dbProps.getProperty("driver"));
}
return java.sql.DriverManager.getConnection(dbProps.getProperty("url"),
dbProps.getProperty("user"), dbProps.getProperty("password"));
}
// test class
public static void main(String args[]) {
ConnectDatabase dbtase;
try {
dbtase = new ConnectDatabase();
System.out.println(dbtase.getConnection());
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
// database.conf.xml(example of connecting to mssql se)
<database-conf>
<datasource>
<driver>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver>
<url>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=[databasename]</url>
<user>[user]</user>
<password>[password]</password>
</datasource>
</database-conf>
Over