如何在 Java 中为每个对象创建一列并添加另一行(如果第一行已填充)

发布于 2025-01-09 04:07:02 字数 1320 浏览 1 评论 0原文

我正在使用 MySQL 编写 Java Servlet。我想以每行有五列的方式显示数据。这就是我想要的桌子。

表我想要

目前,我有 6 个数据项,我将它们放入列表中,因此每个列表中有 5 个项目。 [a、b、c、d、e]、[f]

table

这是我当前拥有的表格。我不确定如何以我想要的方式显示每个项目。

        //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>");

有人可以帮忙吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2025-01-16 04:07:02

这是演示代码。它将在 Servlet 或 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>");

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>");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文