Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
要从数据库中检索java中xml标记的值吗_Java_Mysql_Sql_Xml_Jdbc - Fatal编程技术网

要从数据库中检索java中xml标记的值吗

要从数据库中检索java中xml标记的值吗,java,mysql,sql,xml,jdbc,Java,Mysql,Sql,Xml,Jdbc,嗨,我想使用java编码从SQL数据库中检索xml标记的值 我已经写了一个连接数据库的代码。。。我可以查询数据库。。。使用documentbuliderfactory和transformerfactory创建了一个.xml文件 public class New void condb() { /*written code to connect databse*/ try { /*code for get xml tag

嗨,我想使用java编码从SQL数据库中检索xml标记的值 我已经写了一个连接数据库的代码。。。我可以查询数据库。。。使用documentbuliderfactory和transformerfactory创建了一个.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:type=和xlink:show=的值。。。我尝试声明变量名并为其分配查询,在字段中调用该变量名。。但它不起作用。。。请任何人帮我解决这个问题。

据我所知,您需要将数据库中的公司列表存储在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查询是公司a表还是其他一些select查询?

如果您显示问题代码,您会得到更多帮助。这解决了我一半的问题。我可以访问一个属性的值。。。但对于第二个属性,显示索引超出范围…公司名称为tabale。。。我给taglist赋予了相同的名称,以更正您的查询,以按要求的顺序从company表中选择所有必需的属性值。rs.getXXX需要列索引,但您的查询只返回一列。我不理解上面的行。。。。。。我有两列CId和Cname。我把rs.getstring1和rs.getString2放在一起。但它在连接到索引2时给出以下错误异常超出范围。索引2超出范围。我又尝试了一个while循环。然后,它在连接到结果集时给出以下错误异常即被关闭。结果集已关闭。 select CId from company