Java、XML与数据库编程实践

来源:互联网 发布:哪些智障广告知乎 编辑:程序博客网 时间:2024/06/12 01:29

作者:sirix  Emailnevinguo@163.com

 

在开始学习XML和数据库编程时,大家都对一大堆的文档和资料,无从入手。作者在工作中,正好要用到了这些,就手头的一个程序进行整理。其功能很简单,得用java语言,从access数据库中,把一些数据导入到SQL数据库中。

需求:

Access数据库表结构:

表:production

   产品型号  字符串型………产品编号

   零件图号  字符串型……….零件编号

   图号      字符串型……….工具编号

SQL数据表结构:

Project    产品表

   Id         int           标识

   Number…  varchar64     编号

Product   零件表

   Id         int           标识

   pid         int           产品标识号(与project表中的id相关联)

   Number…  varchar64     编号

Componenttype  与零件对应的生产工具表

   Id         int           标识

   aid         int           零件标识(与product表中的id关联)

   Number…  varchar64     编号

要求把原来的编号分别放到下面的SQL三个表格中,并用id关联起来。

考虑到数据库连接可能有变化,决定使用xml来配置数据连接。Dbs.xml文件内容如下:

<?xml version="1.0" encoding="GBK"?>

<dbcopy>  

 <source>

   <class>sun.jdbc.odbc.JdbcOdbcDriver</class>

   <url>jdbc:odbc:asrs</url>

   <user>""</user>

   <password>""</password>

 </source>

 

 <dest>

   <class>com.microsoft.jdbc.sqlserver.SQLServerDriver</class>

   <url>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=vxms</url>

   <user>vxms</user>

   <password>vxms</password>

 </dest>

</dbcopy>

 

文件ConnPara.java,表示以类形式表示的数据库连接参数。

publicclass ConnPara

{

   String dbClass=null;

   String url=null;

   String username=null;

   String password=null; 

   

   public ConnPara()  {  }

   

   public ConnPara(String pdbClass,String purl,String pusername,String ppassword)

   {

       dbClass=pdbClass;

       url=purl;

       username=pusername;

       password=ppassword;    

   }

   

   public String getDbClass(){returndbClass;}

 

   public String getUrl(){returnurl;}

   

   public String getUsername(){returnusername;}

   

   public String getPassword(){returnpassword;}

 

   public void setDbClass(String str){ dbClass=str;}

 

   public void setUrl(String str){ url=str;}

   

   public void setUsername(String str){username=str;}

   

   public void setPassword(String str){password=str;}

   

}

 

文件DbXmlParser.java封装了对xml文件的操作。

 

importjavax.xml.parsers.*;

importorg.w3c.dom.*;

importorg.xml.sax.*;

importjava.io.*;

 

publicclass DbXmlParser

{

   

   static String  xmlfile;

   public DbXmlParser(String filename)

   {

       xmlfile=filename;

   }

   

   public static Element loadDocument()

   {

       try

       {

           //工厂

           DocumentBuilderFactory dcfactory=DocumentBuilderFactory.newInstance();

           //文档构造器

           DocumentBuilder db=dcfactory.newDocumentBuilder();

           //构造的文档

           Document doc=db.parse(xmlfile);

           //根元素

           Element root=doc.getDocumentElement();

           return root;

           }catch(ParserConfigurationException e){

           System.out.println("ParserConfigurationException");

           e.printStackTrace();

       }catch(IOExceptione)      {

           System.out.println("IOException ");

           e.printStackTrace();

       }catch(SAXExceptione)     {

           System.out.println("SAXException ");

           e.printStackTrace();

       }catch(Exceptione) {

           e.printStackTrace();

       }

       return null;

   }

   

   public ConnPara getSource()

   {

       Element root=loadDocument();

       if(root==null){  returnnull;  }

       NodeList nodes=root.getElementsByTagName("source");

       if(nodes.getLength()>0)

       {      

           Node node=nodes.item(0);

           String connclass=getChildElementValue(node,"class");

           String url=getChildElementValue(node,"url");

           String username=getChildElementValue(node,"user");

           String password=getChildElementValue(node,"password");

           returnnew ConnPara(connclass,url,username,password);

       }

       return null;      

   }  

   

   public ConnPara getDest()

   {

       Element root=loadDocument();

       if(root==null)return null;

       NodeList nodes=root.getElementsByTagName("dest");

       if(nodes.getLength()>0)

       {      

           Node node=nodes.item(0);

           String connclass=getChildElementValue(node,"class");

           String url=getChildElementValue(node,"url");

           String username=getChildElementValue(node,"user");

           String password=getChildElementValue(node,"password");

           return new ConnPara(connclass,url,username,password);

       }

       return null;      

   }

       

   //得到子元素的值

   private String getChildElementValue(Nodenode,String subTagName)

   {

           String returnString= "";

           if(node!= null)

           {

               NodeList children= node.getChildNodes();

               for(intinnerLoop = 0; innerLoop <children.getLength(); innerLoop++)

               {

                   Node child= children.item(innerLoop);

                   if(child== null|| child.getNodeName()== null|| !child.getNodeName().equals(subTagName))

                       continue;

                   Node grandChild= child.getFirstChild(); 

                   if(grandChild.getNodeValue()!= null)

                       return grandChild.getNodeValue();

               }

           }

           return returnString;      

   }  

}

原创粉丝点击