gpt4 book ai didi

java - 行中的结果集输出将转换为以逗号分隔的单列输出

转载 作者:太空宇宙 更新时间:2023-11-04 09:33:19 25 4
gpt4 key购买 nike

我正在尝试编写此java过程,以在用逗号分隔的单个列中显示此代码的输出,而不是在多个行中。在 printOut 部分,我想将行中的所有输出合并为每个用户 1 列。我尝试使用 ArrayList arr = new ArrayList() 和设置 results = new HashSet() ,以便我可以获得数组中以逗号分隔的所有行值,如预期输出所示。

         ResultSet group=(ResultSet)Groups.getFieldValue(USER_GROUP_RESULTSET);//Gets all the user groups for the user
printOut("Groupscount:"+group.getRowCount()); //Counts the # of user groups user is assigned to for printing purpose only
group.moveFirst();
while(!group.isEof()) { //For all the user groups,list the user name and another col with all user groups separated by comma instead of individual rows.
String groupname1= group.getFieldValueString(USER_GROUP); // fetches the user group name in the string. Do i need to use array here?
printOut(USER+";"+groupname1); // It displays the output row wise
group.moveNext();

}

实际输出:
用户1;XYZ
用户1;ABC

预期输出:用户1; XYZ,ABC

最佳答案

您需要将 printOut 移到 while 循环之外

StringBuilder sb=new StringBuilder();
while(!group.isEof()) {
String groupname1=group.getFieldValueString(USER_GROUP);
sb.append(";"+groupname1);
group.moveNext();
}

printOut(USER+sb.toString());

关于java - 行中的结果集输出将转换为以逗号分隔的单列输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56820615/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com