行中的结果集输出将转换为以逗号分隔的单个 col 输出

Resultset output in rows to be converted to single col output separated by comma

我正在尝试编写此 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());