Search code examples
javaresultset

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


I am trying to write this java procedure to display the output of this code in a single col separated by comma instead of multiples rows. In the printOut section, I would like to combine all the output in the rows to 1 col per user. I tried using the ArrayList arr = new ArrayList() and Set results = new HashSet() so that i can get all the row values in an array seperated by comma as shown in the expected output.

         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();

                }

Actual output:
User1;XYZ
User1;ABC

Expected output: User1; XYZ,ABC


Solution

  • You need to move printOut outside of the while loop

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