使用带有xml文件的jdbc与数据库建立连接

时间:2014-04-26 09:56:21

标签: java xml jdbc

我在项目中遇到了一个问题。我必须读取一个xml文件,并根据我从xml获得的一些值,我必须获取一个查询,其中它应该根据xml中的匹配值获取值。

 public class XMLParser extends HttpServlet 

  {


   HashMap<String,String>hMap1=new HashMap<String, String>();
   HashMap<String,String>hMap2=new HashMap<String, String>();

    String series1,series2,ID;
       int s1, s2;

    public void getAllUserNames(String fileName) 
    {
    try {
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        File file = new File(fileName);
        if (file.exists())
        {
            Document doc = db.parse(file);
            Element docEle = doc.getDocumentElement();
            System.out.println("Root element of the document: "+ docEle.getNodeName());
            NodeList studentList = docEle.getElementsByTagName("emplopyee");


            System.out.println("Total emplopyee: " + studentList.getLength());

            if (studentList != null && studentList.getLength() > 0) 
            {
                for (int i = 0; i < studentList.getLength(); i++) 
                {

                    Node node = studentList.item(i);

                    if (node.getNodeType() == Node.ELEMENT_NODE) 
                    {
                        Element e = (Element) node;


                        NodeList nodeList1 = e.getElementsByTagName("ID");
                        ID= nodeList1.item(0).getChildNodes().item(0).getNodeValue();
                        System.out.println("ID:  "+nodeList1.item(0).getChildNodes().item(0).getNodeValue());

                        NodeList nodeList2 = e.getElementsByTagName("Name");
                       series1 =nodeList2.item(0).getChildNodes().item(0).getNodeValue();
                        System.out.println("Name: "+ nodeList2.item(0).getChildNodes().item(0).getNodeValue());

                    NodeList    nodeList3 = e.getElementsByTagName("Days");
                        series2 = nodeList3.item(0).getChildNodes().item(0).getNodeValue();
                        System.out.println("Days: "+ nodeList3.item(0).getChildNodes().item(0).getNodeValue());

                       hMap1.put(ID,series1);
                         hMap2.put(ID, series2);
                        System.out.println(hMap1);
                        System.out.println(hMap2)

                        Class.forName("com.mysql.jdbc.Driver");
                        Connection con =   DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root",null);
                        Statement st=con.createStatement();
                        ResultSet rs=st.executeQuery("Select * from emp1 where eid ='"+ID+"' ");


                   while(rs.next())
                  {
                    int n1 = Integer.parseInt(rs.getString(1));
                    String n2 = rs.getString(2);
                    int n3 = Integer.parseInt(rs.getString(3));
                    int n4 =  Integer.parseInt(rs.getString(4));
                    System.out.println(n1);
                    System.out.println(n2);
                    System.out.println(n3);
                    System.out.println(n4);
                    }
                    }
                }
            } 
            else {
                System.exit(1);
            }
        }
    } catch (Exception e) {
        System.out.println(e);
    }
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, java.io.IOException 
        {
            XMLParser parser = new XMLParser();
            parser.getAllUserNames("D:\\employee.xml");
         }
      }

0 个答案:

没有答案
相关问题