Home > Net >  How to have a column with each object and add another row if the first row is filled in Java
How to have a column with each object and add another row if the first row is filled in Java

Time:02-28

I am writing Java Servlet using MySQL. I want to display data in such a way that in each rows there are five columns. This is the table i want.

table that i want

Currently, I have six data items and i put them in list so in each list there are 5 items. [a,b,c,d,e] , [f]

table

This is the table i currently have. I am not sure how to show each item in the way i want.

        //getting sql data
        List<Object> dataItems = new ArrayList<Object>();
        while (rset.next()) {
            SQLData item = new SQLData(rset.getString("name"), rset.getString("prod_desc"),
                    rset.getDouble("price"));
            dataItems.add(item);
        }

        //chuck list
        int size = 5;
        List<Object>[] partition = partition(dataItems, size);

        out.println("<div class='container' style='border:1px solid #cecece;'>");

        out.println("<div class='row'>");
        for (Object item : partition) {
            out.println("<div class='col'>");
            out.println("<p>"   item   " test </p>");
            out.println("</div>");
        }
        out.println("</div>");

        out.println("</div>");

Anyone can help?

CodePudding user response:

Here is demonstration code. It will work in a Servlet or a JSP.

    List<String> dataItems = List.of("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p");
    out.println("<table>");
    out.println("<tr>");
    for (int i = 0; i < dataItems.size(); i  ) {
        if(i >= 5 && i % 5 == 0){
            out.println("</tr><tr>");
        }
        out.println("<td> "   dataItems.get(i)   " </td>");
    }
    out.println("</tr>");
    out.println("</table>");
  • Related