单列+JDom+XML

Posted on 2008-10-17 14:50 橡皮人 阅读(217) 评论(0)  编辑  收藏
  使用JDom做一个数据屏蔽的例子
 
  XML代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<sys-info>
    
<sqlinfo>
        
<driver>com.mysql.jdbc.Driver</driver>
        
<url>jdbc:mysql://localhost:3306/address</url>
        
<username>root</username>
        
<password>nicholas</password>
    
</sqlinfo>
    
<programeinfo>
        
<beans>
            
<bean id="com.nicholas.inter.IUserDAO" class="com.nicholas.inter.imple.UserDAO"/>
        
</beans>
    
</programeinfo>
</sys-info>
其中bean的信息使用属性来封装,这样做比多个XML对象要灵活一些,我的意图是用Util下的Map装载这些信息,实现父类引用指向子类对象。

写一个JDBC访问对象的Bean
public class SysInfo {

    
private String driver;
    
private String url;
    
private String username;
    
private String password;

    
public SysInfo() {
    }

    
public String getDriver() {
        
return driver;
    }

    
public void setDriver(String driver) {
        
this.driver = driver;
    }

    
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;
    }
}


现在开始使用JDom解析XML的数据


public class ParseMessage {

    SAXBuilder builder 
= null;
    Document doc 
= null;
    Element el_root 
= null;
        
//创建单列模式
    private static ParseMessage instance;

    
private ParseMessage() {
        builder 
= new SAXBuilder();
        
try {
            doc 
= builder.build("config.xml");
            el_root 
= doc.getRootElement();
        } 
catch (JDOMException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (IOException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    
public static ParseMessage getInstance() {

        
if (instance == null) {

            instance 
= new ParseMessage();

        }
        
return instance;

    }

    
public SysInfo getSysInfo() {
        SysInfo si 
= null;
        
try {
            Element el_driver 
= (Element) 

XPath.selectSingleNode(el_root,
                    
"//driver");
            Element el_url 
= (Element) 

XPath.selectSingleNode(el_root, 
"//url");
            Element el_username 
= (Element) 

XPath.selectSingleNode(el_root,
                    
"//username");
            Element el_password 
= (Element) 

XPath.selectSingleNode(el_root,
                    
"//password");
            String driver 
= el_driver.getText();
            String url 
= el_url.getText();
            String username 
= el_username.getText();
            String password 
= el_password.getText();
            si 
= new SysInfo();
            si.setDriver(driver);
            si.setUrl(url);
            si.setUsername(username);
            si.setPassword(password);
        } 
catch (JDOMException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        
return si;

    }

    
public Map initBeans() {
        List list 
= null;
        Map map 
= new HashMap();
        
try {
            list 
= XPath.selectNodes(el_root, "//bean");
        } 
catch (JDOMException e) {
            
            e.printStackTrace();
        }
        
int index = list.size();
        
for (int i = 0; i < index; i++) {
            Element el_parameter 
= (Element) list.get(i);
            Attribute att_inter 
= 

el_parameter.getAttribute(
"id");
            Attribute att_imple 
= 

el_parameter.getAttribute(
"class");
            String inter 
= att_inter.getValue();
            String imple 
= att_imple.getValue();
            map.put(inter, imple);
        }
        
return map;

    }
    
    
public UserDAO getUserDAO(Class c) {
        UserDAO userdao
=null//用户数据访问层
        Map map=this.initBeans();
        
try {
            userdao
=(UserDAO)Class.forName(map.get

(c.getName()).toString()).newInstance();
        } 
catch (InstantiationException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (IllegalAccessException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        } 
catch (ClassNotFoundException e) {
            
// TODO Auto-generated catch block
            e.printStackTrace();
        }
        
return userdao;
        
    }

}


DB层依靠ParseMessage类解析出来的数据获得Driver等等

    static SysInfo si=ParseMessage.getInstance().getSysInfo();
    
public static Connection getConn() throws Exception {
        
//1 装驱动
        Class.forName(si.getDriver());
        Connection conn 
= DriverManager.getConnection(si.getUrl(), si.getUsername(), si.getPassword());
        
return conn;
    }

Servlet依靠JSP页面传递过来的Username Password进行判断

response.setContentType("text/html;charset=gbk");
        String username
=request.getParameter("username");
        String password
=request.getParameter("password");
        User user
=new User(); //User bean
        user.setUsername(username);
        user.setPassword(password);
        UserDAO userdao
=ParseMessage.getInstance().getUserDAO

(IUserDAO.
class); //传递UserDAO父接口
        if(userdao.isRight(user)) {
            
            System.out.println(
"登录成功!");
        } 
else {
            System.out.println(
"登录失败!");
            
        }

其中应该注意的是XML的路径问题,使用Tomcat配置项目的时候并没有把XML文件导入到项目的子文件中,根据需要可以把XMl文件放到SRC中,修改ParseMessage中的路径即可,因为是写小例子,我直接把XML文件放到Tomcat目录下的Bin文件夹中,Tomcat默认会查找Bin文件夹。

初次写Java单列和JDom,刚写的时候非常不习惯,但是要达到屏蔽数据的效果,可以考虑使用。


只有注册用户登录后才能发表评论。


网站导航:
 

posts - 28, comments - 5, trackbacks - 0, articles - 0

Copyright © 橡皮人