首页 > 解决方案 > 带有下一个值的while循环

问题描述

我想在 while 循环中获取键和值

while (rs.next()) {
     String simpleData = "<SimpleData name="akey">avalue</SimpleData>\n";
}

我需要掌握所有的关键和价值。如果我在结果集中有 10 个值,那么简单数据应该包含所有键和值。像下面

输出:- 最后我的字符串应该像下面的字符串

 simpleData = "<SimpleData name="acolumnname">acolumnvalue</SimpleData>
               <SimpleData name="bcolumnname">bcolumnvalue</SimpleData>
             …";

我怎样才能实现

标签: javawhile-loop

解决方案


如果你想手动创建一个 xml 结构(这意味着不使用合适的库),你可以尝试这样的事情:

public static void main(String[] args) {
    ResultSet rs = // however you get it

    // get the meta data of the result set, they are including the column headers
    ResultSetMetaData resultSetMetaData = rs.getMetaData();
    // and get the first column header
    String columnHeader = resultSetMetaData.getColumnLabel(1);

    // initialize an empty StringBuilder OUTSIDE the loop
    StringBuilder sb = new StringBuilder();
    // then loop through the resultset
    while (rs.next()) {
        // appending the results to the StringBuilder
        sb.append("<SimpleData name=\"")    // opening tag plus xml attribute name
        .append(columnHeader)               // column header as determined before the loop
        .append("\">")                      // close the opening tag and the attribute value
        .append(rs.getString(1))            // get the value from the result set
        .append("</SimpleData>")            // write the closing tag
        .append(System.lineSeparator());    // append a line break
    }

    System.out.println(sb.toString());
}

这应该打印一个 xml 结构(希望是所需的):

<SimpleData name="column header">value</SimpleData>

编辑
原来你想为只有一行的结果集的每一列值创建一个 xml 节点。那(几乎完全)不同......然后我将通过它们的别名(标题/标签)而不是它们的索引来访问列:

public static void main(String[] args) throws SQLException {
    ResultSet rs = null; // however you get it
    // create a container for the headers
    List<String> columnHeaders = new ArrayList<>();
    // get the meta data of the result set, they are including the column headers
    ResultSetMetaData resultSetMetaData = rs.getMetaData();
    // determine the amount of columns
    int columnCount = resultSetMetaData.getColumnCount();

    // iterate them and store their values in a list of strings
    for (int i = 1; i <= columnCount; i++) {
        columnHeaders.add(resultSetMetaData.getColumnLabel(i));
    }

    // initialize an empty StringBuilder OUTSIDE the loop
    StringBuilder sb = new StringBuilder();
    // then loop through the resultset
    while (rs.next()) {
        // now loop through the columnHeaders
        for (String header : columnHeaders) {
            // append each column result to the StringBuilder as a single xml node
            sb.append("<SimpleData name=\"")    // opening tag plus xml attribute name
            .append(header)                     // column header as determined before the loop
            .append("\">")                      // close the opening tag and the attribute value
            .append(rs.getString(header))       // get the value from the result set by header, not index
            .append("</SimpleData>")            // write the closing tag
            .append(System.lineSeparator());    // append a line break              
        }
    }

    System.out.println(sb.toString());
}

推荐阅读