2.建立3个类和连接数据库的xml文件,目录如下图:
3.代码分析:
(1)连接数据库的xml文件:
<?xml version="1.0" encoding="UTF-8"?> <config> <db-info> <driver-name>oracle.jdbc.driver.OracleDriver</driver-name> <url>jdbc:oracle:thin:@localhost:1521:orcl</url> <user-name>drp1</user-name> <password>drp1</password> </db-info> </config>
(2)读取xml文件的类:XmlConfigReader.java
public class XmlConfigReader { //懒汉式 private static XmlConfigReader instance = null; //保存jdbc相关的配置信息 private JdbcConfig jdbcConfig = new JdbcConfig(); private XmlConfigReader(){ SAXReader reader = new SAXReader(); InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("sys-config.xml"); try { Document doc = reader.read(in); //取驱动的名字 Element driverNameElt = (Element)doc.selectObject("/config/db-info/driver-name"); //取得jdbc相关的配置 jdbcConfig.setDriverName(driverNameElt.getStringValue()); //取url Element urlElt = (Element)doc.selectObject("/config/db-info/url"); //String url = urlElt.getStringValue(); //取得jdbc相关的配置 jdbcConfig.setUrl(urlElt.getStringValue()); //用户名称 Element userNameElt = (Element)doc.selectObject("/config/db-info/user-name"); //String userName = userNameElt.getStringValue(); //取得jdbc相关的配置 jdbcConfig.setUserName(userNameElt.getStringValue()); //密码 Element passwordElt = (Element)doc.selectObject("/config/db-info/password"); //取得jdbc相关的配置 jdbcConfig.setPassword(passwordElt.getStringValue()); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //保证线程为一 public static synchronized XmlConfigReader getInstance(){ if(instance == null){ instance = new XmlConfigReader(); } return instance; } /** * 返回jdbc相关配置 * @return */ public JdbcConfig getJdbcConfig(){ return jdbcConfig; } public static void main(String[] args){ JdbcConfig jdbcConfig = XmlConfigReader.getInstance().getJdbcConfig(); System.out.println(jdbcConfig); } }
package com.zzj.drp.util; /** * jdbc配置 * @author zzj * */ public class JdbcConfig { private String driverName; private String url; private String userName; private String password; public String getDriverName() { return driverName; } public void setDriverName(String driverName) { this.driverName = driverName; } 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; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return this.getClass().getName()+ "{driverName:"+ driverName + ", url:" + url + ", uerName:" + userName + "}"; } }
(4)DbUtil(JDBC)类
public class DbUtil { /** * 取得Connection * @return */ public static Connection getConnection(){ Connection conn = null; try { JdbcConfig jdbcConfig = XmlConfigReader.getInstance().getJdbcConfig(); Class.forName(jdbcConfig.getDriverName()); conn=DriverManager.getConnection(jdbcConfig.getUrl(), jdbcConfig.getUserName(), jdbcConfig.getPassword()); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } }
通过将数据库的连接字符串放到xml中,这样可以灵活配置,xml文件就像一个接口,很好的体现了“面向接口”的思想。