?? connectionproperty.java.svn-base
字號:
/**
* Copyright: Copyright (c) 2004 Handson
* Description: 錕斤拷db-config錕僥鹼拷錕叫伙拷取錕斤拷菘錕斤拷錕斤拷錕斤拷錕較?
* Title: ConnectionProperty.java
* @author Administrator
* Create Time: 錕斤拷錕斤拷10:05:30
* @Version:1.0
*/
package cn.handson.model.connection;
import java.io.IOException;
import java.util.logging.Logger;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
//import cn.handson.framework.util.FileClassPath;
public class ConnectionProperty {
private static Logger log = Logger.getLogger("ConnectionProperty");
private static final String XML_FILE = "/resources/dbconfig.xml";
private static ConnectionProperty instance = new ConnectionProperty();
private String driverClassName = "";
private String url = "";
private String user = "";
private String password = "";
private String providerType = "";
private String jndiName = "";
private String dbms = "";
/**
* Description: 錕斤拷錕斤拷模式錕斤拷錕斤拷錕斤拷一錕斤拷ConnectionProperty錕斤拷實錕斤拷
* @return ConnectionProperty錕斤拷實錕斤拷
*/
public static ConnectionProperty getInstance(){
if( instance == null ){
instance = new ConnectionProperty();
}
return instance;
}
/**
* Description錕斤拷錕斤拷錕斤拷Dom錕斤拷錕斤拷錕斤拷菘錕斤拷錕斤拷錕斤拷錕斤拷錕斤拷募錕?
*/
private ConnectionProperty(){
//FileClassPath fcp = new FileClassPath();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
Document doc = null;
try {
String xmlFile = this.getClass().getResource(XML_FILE).getPath();
builder = factory.newDocumentBuilder();
doc = builder.parse(xmlFile);
} catch (ParserConfigurationException e) {
log.warning(e.getMessage());
} catch (SAXException e) {
log.warning(e.getMessage());
} catch (IOException e) {
log.warning(e.getMessage());
}
Element root = doc.getDocumentElement();
providerType = root.getAttribute("type");
dbms = root.getAttribute("dbms");
if( providerType.equals("connection_pool") ){
jndiName = root.getAttribute("jndi");
}
else if( providerType.equals("default") ){
driverClassName = value( root, "driver_class_name" );
url = value( root, "url" );
user = value( root, "user" );
password = value( root, "password" );
}
report();
}
/**
* Description: 錕斤拷印錕斤拷菘錕絣錕接憋拷錕斤拷
*/
private void report(){
System.out.println("Connection provider type is " + providerType);
if(providerType.equals("connection_pool")){
if (jndiName != null && !jndiName.equals("")) {
System.out.println("JNDI name is " + jndiName);
}
else {
System.out.println("Not found jndi Name!");
}
}
else{
System.out.println(this.driverClassName + "\n" + this.url + "\n" +
this.user + "\n" + this.password);
}
}
/**
* Description:
* @param element
* @param tagName
* @return
*/
private String value( Element element, String tagName ){
Element e = (Element)element.getElementsByTagName(tagName).item(0);
Text text = (Text) e.getFirstChild();
return text.getNodeValue();
}
public String getProviderType(){
return providerType;
}
public String getJndiName(){
return jndiName;
}
public String getDriverClassName(){
return driverClassName;
}
public String getUrl(){
return url;
}
public String getUser(){
return user;
}
public String getPassword(){
return password;
}
public String getDBMS(){
return dbms;
}
//Test
public static void main(String []args){
ConnectionProperty cp = ConnectionProperty.getInstance();
System.out.println(cp.getJndiName());
System.out.println(cp.getDriverClassName());
System.out.println(cp.getPassword());
System.out.println(cp.getUrl());
System.out.println(cp.getUser());
System.out.println(cp.getProviderType());
System.out.println(cp.getDBMS());
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -