2011-09-21 47 views
1

您好我想从SQL数据库检索值为XML标记使用java编码 我写了一个代码连接数据库...我能够查询数据库.. 。TYPE =“‘和XLink的:秀=’”从数据库...我想声明使用documentbuliderfactory和transformerfactory.Now输出是这样想要检索数据库中的值为XML在XML中的标记

public class New 

     void condb() { /*written code to connect databse*/ 
      try { 
       /*code for get xml tags*/ 
           stmt = connection.createStatement(); 
       String querystring = ("select CId from company"); 
       rs = stmt.executeQuery(querystring); 
       System.out.println("\n" + "CId"); 
       while (rs.next()) { 
        System.out.println(rs.getInt(1) + " "); 
       } 
       Element child1 = doc.createElement("Company"); 
       child1.setAttributeNS(xlink, "xlink:type", "");           child1.setAttributeNS(xlink, "xlink:show", " "); 
       child.appendChild(child1); 
       /*code for xml*/ 
       bw.flush(); 
       bw.close(); 
      } catch (Exception e) { 
       System.out.println("Exception in connecting to DB" + e.getMessage()); 
       System.err.println(e.getMessage()); 
      } 
     } 

     public static void main(String args[]) throws Exception { 
      New e = new New(); 
      e.condb(); 
     } 
} 

我想要得到的XLink值创建一个.xml文件变量名称并为其分配查询,在“”字段中调用该变量名称,但它不起作用...请任何人都可以帮助我解决这个问题。

+0

如果您显示问题代码,您将获得更多帮助。 –

+0

编辑的问题代码 – Sharada

回答

0

据我所知,您需要在数据库中存储xml公司列表。 试试这个:

  while (rs.next()) 
      { 
       Element child1 = doc.createElement("Company"); 
       child1.setAttributeNS(xlink, "xlink:type", rs.getString (1)); 
       child1.setAttributeNS(xlink, "xlink:show", " "); 
       child.appendChild(child1); 
      } 

虽然我不太明白您的SQL查询

select CId from company
是公司一个表或一些其他的选择查询?

+0

这解决了我的一半问题。我可以访问一个属性的值...但对于第二个属性,这显示索引超出范围... – Sharada

+0

公司是tabale名称...并且我也对taglist也给予了相同的名称 – Sharada

+0

更正您的查询以从所有必需的属性值中选择公司表按要求的顺序。 rs.getXXX需要列索引,但查询只返回一列。 –