<sys-configure>
<jdbc-info>
<driver-class-name>oracle.jdbc.driver.OracleDriver</driver-class-name>
<url>jdbc:oracle:thin:@127.0.0.1:1521:yqs</url>
<user-name>root</user-name>
<password>yqsshr</password>
</jdbc-info>
<beans>
<bean id="com.yqs.drp.basedata.dao.ItemDao" class="com.yqs.bbs.basedata.dao.ItemDao.ClassName" />
<bean id="com.yqs.bbs.basedata.dao.FlowCardDao" class="com.yqs.bbs.basedata.dao.FlowCardDao.ClassName" />
<!-- 可把所有的Dao配置进来 -->
</beans>
</sys-configure>
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.Element;
import org.jdom.xpath.*;
private static GlobalConfigure instance = new GlobalConfigure();
private static final String CONFIG_FILE_NAME = "gobal-configure.xml";
private Element rootElt;
private JdbcInfo jdbcInfo = new JdbcInfo(); //JavaBean对象
private GlobalConfigure() {
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(Thread.currentThread().getContextClassLoader().getResourceAsStream(CONFIG_FILE_NAME)); //读取文件
initJdbcInfo();
} catch (JDOMException e) {
System.out.println("jdom error");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("io error");
e.printStackTrace();
}
}
private static GlobalConfigure getInstance() {
return instance;
}
private void initJdbcInfo() {
try {
Element driverClassNameElt = (Element)XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/driver-class-name");
Element urlElt = (Element)XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/url");
Element userNameElt = (Element)XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/user-name");
Element passwordElt = (Element)XPath.selectSingleNode(rootElt, "//sys-configure/jdbc-info/password");
//将内容放到JavaBean
jdbcInfo.setDriverClassName(driverClassNameElt.getText());
jdbcInfo.setUrl(urlElt.getText());
jdbcInfo.setPassword(passwordElt.getText());
jdbcInfo.setUserName(userNameElt.getText());
//System.out.println(driverClassNameElt.getText());
//System.out.println(urlElt.getText());
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println(GlobalConfigure.getInstance()); //打印测试
}
return jdbcInfo;
}
public String toString() {
return "driverClassName:" + jdbcInfo.getDriverClassName() +
"\nURL:" + jdbcInfo.getUrl() +
"\nusername:" + jdbcInfo.getUserName() +
"\npassword:" + jdbcInfo.getPassword();
}
public class JdbcInfo {
private String driverClassName;
private String url;
private String userName;
private String password;
public String getDriverClassName() {
return driverClassName;
}
public void setDriverClassName(String driverClassName) {
this.driverClassName = driverClassName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
程序运行结果如下:
URL:jdbc:oracle:thin:@127.0.0.1:1521:yqs
username:root
password:yqsshr