java - 带有下一个值的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>
…";
我怎样才能实现
解决方案
如果你想手动创建一个 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());
}
推荐阅读
- html - 使用角度打字稿在组件的初始化时启动文件输入的自动单击
- javascript - 表单提交成功时Jquery AJAX显示网页
- sql - Redshift 查询删除 6 个月大的数据
- python - Pandas:TypeError:在日期列上选择时,“int”和“str”实例之间不支持“>”
- c# - SelectedIndex 没有改变 SharePoint asp DropDownList
- r - 在 ggplot 中绘制置信区间(来自矩阵)
- ruby-on-rails - 如何处理 after_create 错误?(尝试创建 webhook)
- r - 如何在保留矩阵结构的情况下在 R 中创建热图矩阵?
- c++ - 为什么不能实例化原子对?
- javascript - 检查自 NodeJS 中上次间隔以来更改的属性