Java 使用下一个值进行while循环
我想在while循环中获取键和值Java 使用下一个值进行while循环,java,while-loop,Java,While Loop,我想在while循环中获取键和值 while (rs.next()) { String simpleData = "<SimpleData name="akey">avalue</SimpleData>\n"; } while(rs.next()){ 字符串simpleData=“avalue\n”; } 我需要带上所有的钥匙和价值观。如果resultset中有10个可用值,那么简单数据应该包含所有键和值。如下 输出:-最后,我的字符串应该如下所示 串 s
while (rs.next()) {
String simpleData = "<SimpleData name="akey">avalue</SimpleData>\n";
}
while(rs.next()){
字符串simpleData=“avalue\n”;
}
我需要带上所有的钥匙和价值观。如果resultset中有10个可用值,那么简单数据应该包含所有键和值。如下
输出:-最后,我的字符串应该如下所示
串
simpleData = "<SimpleData name="acolumnname">acolumnvalue</SimpleData>
<SimpleData name="bcolumnname">bcolumnvalue</SimpleData>
…";
simpleData=“acolumnvalue
B列值
…";
如何在while循环之外以及在您应该通过附加到simpleData的每次迭代中实现声明simpleData+=
String simpleData ;
int i ;
while (rs.next()) {
simpleData += "<SimpleData name="+key+" "+rs.getString(i)+"</SimpleData>\n";
i++;
}
String simpleData;
int i;
while(rs.next()){
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());
}
publicstaticvoidmain(字符串[]args){
ResultSet rs=//无论您如何获得它
//获取结果集的元数据,它们包括列标题
ResultSetMetaData ResultSetMetaData=rs.getMetaData();
//并获取第一列标题
String columnHeader=resultSetMetaData.getColumnLabel(1);
//在循环外部初始化空StringBuilder
StringBuilder sb=新的StringBuilder();
//然后循环遍历结果集
while(rs.next()){
//将结果附加到StringBuilder
sb.append(“”//关闭开始标记和属性值
.append(rs.getString(1))//从结果集中获取值
.append(“”//写入结束标记
.append(System.lineSeparator());//追加换行符
}
System.out.println(sb.toString());
}
这应该是打印xml结构(希望是所需的):
值
编辑
结果是,您希望为结果集中只有一行的每个列值创建一个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());
}
publicstaticvoidmain(字符串[]args)抛出SQLException{
ResultSet rs=null;//无论您如何获得它
//为标题创建一个容器
List columnHeaders=新建ArrayList();
//获取结果集的元数据,它们包括列标题
ResultSetMetaData ResultSetMetaData=rs.getMetaData();
//确定列的数量
int columnCount=resultSetMetaData.getColumnCount();
//迭代它们并将它们的值存储在字符串列表中
对于(int i=1;我在每次迭代中使用StringBuilder
和append(simpleData)
,如果需要,可以删除最后一个“\n”
。@deaar感谢您的回复。我如何打印键和值rs.getString(0)
和rs.getString(1)
,也许……在结果集中的最后一个键和值出现之前,您还没有提供ResultSet
@deHaar的示例输出,我不知道将有多少个键和值,这就是为什么您使用while(rs.next())
,它可以获取所有结果行…请提供示例outputResultSetMetaData rsmd=rs.getMetaData();rs.getColumnName(colunm_编号);在while循环外部声明变量i,并将i放入其中==>rs.getString(i)然后在ever Iteration中增加i,然后在while循环中只返回一行previsuly,我们再次声明simpleData,如“String simpleData”。这将在每次(每次迭代)时创建一个新字符串。