SAX used in java to connect to database

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值