What is best way to display a results set from a database

What is best way to display a results set from a database

What would be the best way to achieve a JSP which displays say 10-15 product listings from a database?

For example if I wanted to pull (this is basic, reality would be more data, but it gets the point across) all of the "product names" from the database, then display them in a table on the site, one row after another.

My initial thought is to use a JavaBean, although I am no expert on this - and I am not 100% sure how I would implement this solution just yet either.

What are everyones thoughts on the best way to achieve this?

Any help appreciated.

Have your business layer grab the data and return it using a collection of some type. Could be an array, could be a List. What it should not be is a result set! Results sets should be opened and closed as quickly as possible at the lowest levels of the application.

The controller places the collection in request scope for access by the JSP, which uses the <c:forEach> JSLT tag to iterate over it and create the HTML for display.

See the JSP FAQ if paging the data is important to you.

Almost got my head around this JSTL stuff and almost got it working.

I am trying to display the data in a table format on my JSP page, but I cannot seem to get my head around the nested c:forEach as what ever I try the display just doesn't do what it should be doing. So either I have been looking at this for too long and not getting anywhere, or I am doing something wrong.

Servlet:
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped

                                    ArrayList<ArrayList<String>> rows = new ArrayList<ArrayList<String>>(); 
                                    ArrayList<String> columns = new ArrayList<String>(); 
     
                    while (results.next ())  
                    { 
                                       columns.add(results.getString(1)); 
                                       columns.add(results.getString(2)); 
                                       columns.add(results.getString(3)); 
                                        
                                       rows.add(columns); 
                                        
                                       columns.clear(); 
                                      
                    } 
                                  
                    results.close(); 
                    stmt.close(); 
                                     
                                    request.setAttribute("rows", rows); 

 

JSP:
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped

    <table> 
     <c:forEach var="rows" items="${rows}" > 
                <tr> 
                    <td>Row</td> 
                        <c:forEach var="column" items="${rows}" > 
                             
                                <td>Column</td> 
                                <td>${column}</td> 
                     
                        </c:forEach> 
                </tr> 
                 
            </c:forEach> 
    </table> 

 

From my understanding, this should work....but it isn't quite behaving as expected. When I leave the "columns.clear()" line in, then I just get two bits of "Rows" printed on the jsp page, with no actual data (seems like it is clearing the data before adding the "columns" to the "rows"??). Then when I remove that line of code, I get the two rows of data, but in the same column, then repeated again on the second row.

So if I had three rows it would be displayed as....3 rows of data all on one row (col 1, 2, 3 then col 1, 2, 3 or the next row....but all on one row), then the second row would display the same as the first row.

Hope that all makes sense, if not I can take some screens to help explain more.

 

Copyright © 2007-2012 www.chuibin.com Chuibin Copyright