SAX used in java to connect to database

原创 2006年05月19日 13:38:00

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

相关文章推荐

How can I connect Unity to an SQL database in order to implement an MMO?

问: Hello, first of all I'd like to thank each and everyone who has dedicated their time into pr...
  • Koupoo
  • Koupoo
  • 2012年07月12日 15:16
  • 352

On the Criteria To Be Used in Decomposing Systems into Modules

On the Criteria To Be Used in Decomposing Systems into Modules D.L. Parnas Carnegie-Mellon Univers...

PL/SQL Scripts Used in Migrating to ASM Storage

PL/SQL Scripts Used in Migrating to ASM Storage   The following PL/SQL scripts perform tasks which...

Wildcard App IDs can not be used to create in House provisioning profiles.Please use an Explicit App

测试打包时报错Wildcard App IDs can not be used to create in House provisioning profiles.Please use an Expli...

mysql进阶(十七)Cannot Connect to Database Server

Cannot Connect to Database Server 缘由 由于不同的项目中使用的数据库用户名与密码出现了不一致的情况,在其中之前较早一个项目执行过程中出现“The user speci...

nnn to be used in aodv

  • 2010年02月25日 20:05
  • 122KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:SAX used in java to connect to database
举报原因:
原因补充:

(最多只允许输入30个字)