简体   繁体   中英

How to print 2D array in table format in jsp

I retrieved data from db , and i am able to print in console.

Code : 
List<String[]> table = new ArrayList<>();
ResultSet rs2=p2.executeQuery();
            int nCol = rs2.getMetaData().getColumnCount();
            while(rs2.next()){
                 String[] row = new String[nCol];
                    for( int iCol = 1; iCol <= nCol; iCol++ ){
                        row[iCol-1] = rs2.getObject( iCol ).toString();
                    }
                    table.add( row );
            }

            for( String[] row: table ){
                for( String s: row ){
                    System.out.print( " " + s );

                }
               System.out.println();
            }
return table;

Output in Console:
 3002085 -1073741824 2015-02-27 2015-03-27 Balance Group<Subscription> 61917361 B-Free GPRS Bundle - UNIT Bytes
 3000009 -409.1 2015-02-27 2015-03-27 Balance Group<Subscription> 61917361 B-Anytime Dollar bundles - UNIT Dollars
 3500101 -1073741824 2015-02-27 2015-03-27 Balance Group<Subscription> 61917361 B-Aggregated Data Entitlement Counter - UNIT Bytes
 3000009 -409.1 2015-01-27 2015-02-27 Balance Group<Subscription> 61917361 B-Anytime Dollar bundles - UNIT Dollars
 3500101 -1073741824 2015-01-27 2015-02-27 Balance Group<Subscription> 61917361 B-Aggregated Data Entitlement Counter - UNIT Bytes
 3002085 -1073741824 2015-01-27 2015-02-27 Balance Group<Subscription> 61917361 B-Free GPRS Bundle - UNIT Bytes
 3500101 -1073741824 2014-12-27 2015-01-27 Balance Group<Subscription> 61917361 B-Aggregated Data Entitlement Counter - UNIT Bytes
 3000009 -409.1 2014-12-27 2015-01-27 Balance Group<Subscription> 61917361 B-Anytime Dollar bundles - UNIT Dollars

I have returned the entire List table to the servlet where this method is called. In the servlet I use set attribute to pass it to jsp. I dont know how to iterate this list in jsp and print them in a Table tag.

Earlier I tried with arraylist, but for every column result i had to use it. My code while passing array list

jsp code :
<%

ArrayList<String> date=(ArrayList<String>)request.getAttribute("date");
ArrayList<String> packageid=(ArrayList<String>)request.getAttribute("packageid");
ArrayList<String> cli=(ArrayList<String>)request.getAttribute("cli");
ArrayList<String> env=(ArrayList<String>)request.getAttribute("env");

if (cli != null && !cli.isEmpty()){%>
    <table class="table table-hover">
    <thead>
      <tr>
        <th>DATE</th>
        <th>PackageID</th>
        <th>CLI</th>
        <th>ENV</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
    <%for(int i=0;i < cli.size();i++)
    {%>

          <tr>
            <td><%= date.toArray()[i] %></td>
            <td><%= packageid.toArray()[i]%></td>
            <td><%=cli.toArray()[i] %></td>
            <td><%=env.toArray()[i] %></td>
            <td><a href="del.jsp?id=<%=cli.toArray()[i] %>&dt=<%= date.toArray()[i] %>">Delete</a>/<a href="lock.jsp?cli=<%=cli.toArray()[i] %>&dt=<%= date.toArray()[i] %>">Lock</a>
          </tr>





    <%} 

}else{%>
<tr><b><td>No details </td><td>Present in </td><td>Queue to display.</td></b>
</tr>

<%}%>
   </tbody>
      </table> </div></div>

In this similar way can i print my 2D array in table format ?? Kindly help

您可以使用字符串的2D数组来存储表数据,并在jsp页面中显示时通过此2D数组进行迭代。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM