在java项目文件读取数据库文件prodect.xml信息

最近做了一个关于读取项目中prodect.xml文件中数据库连接的信息,这是测试源码,可直接粘用。

public class B {
    public static void main(String[] args) {
        Connection conn ;    
        ResultSet rs = null ;    
        Statement st = null ;
        String ip = "";
        String port = "";
        String dbname = "";
        String user = "";
        String pwd = "";
        //项目路径
        String filePath = String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""))+"product.xml";
        filePath = filePath.replaceAll("file:/", "");
        filePath = filePath.replaceAll("%20", " ");
        filePath = filePath.trim();

        // 创建saxReader对象  
        SAXReader reader = new SAXReader();  
        Document document;
        try {
            document = reader.read(new File(filePath));
            //获取根节点元素对象  
            Element node = document.getRootElement();  

            // 获取根节点下,子节点名称为datasource的元素。  
            Element element = node.element("datasource");  

            // 获取datasource下所有ds的节点
            List<Element> list = element.elements("ds");
            for (Element e : list){
                //获取ds节点下的子节点
                Element link = e.element("link");

                ip = link.attributeValue("ip");
                port = link.attributeValue("port");
                dbname = link.attributeValue("dbname");
                user = link.attributeValue("user");
                pwd = link.attributeValue("pwd");

                System.out.println("ip="+ip+"/port="+port+"/dbname="+dbname+"/user="+user+"/pwd="+pwd);
            }

        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值