File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes JSP creating html rows for displaying data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP creating html rows for displaying data" Watch "JSP creating html rows for displaying data" New topic
Author

JSP creating html rows for displaying data

Stevie Adams
Greenhorn

Joined: Dec 22, 2004
Posts: 3
Hello,

How do you create html table rows with java when displaying data from a database? Keep in mind I'll never know how many rows I'll need since new data will be entered, so in the future the table may need more rows or maybe less rows in the future. So I can't hard code the rows.

So what I want is the java code to create the rows for a table, display the data in the rows, and also place a delete button at the end of each row? Can somebody give me an example so I can understand the process?

Thank You,

Stevie


---------------------------------------
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29249
    
139

Stevie,
Welcome to JavaRanch!

You can use a loop or looping tag to go through each row. On each loop iteration, display one HTML table row.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Stevie Adams
Greenhorn

Joined: Dec 22, 2004
Posts: 3
Hi Jeanne,

Thank You! Yes I figured it would be a loop but can you give me an example to show me how to create the table rows dynamically?
jyothsna kumari
Ranch Hand

Joined: Jul 21, 2003
Posts: 108
Hi,

Code can be something like this.

<table>

<%for(int i=0;i<=count;i++){%>
<tr><td>data</td></tr>
<%}%>

</table>

Here based on the count which will be a dynamic number you can generate the rows.

Thanks,
Jyothsna
Stevie Adams
Greenhorn

Joined: Dec 22, 2004
Posts: 3
I want to display phone numbers. Would this work? -

<table>

<%for(int i=0;i<=count;i++){%>
<tr><td>Phone Numbers</td></tr>
<TD><%= address.getPhonenumber() %>
</TD>
<%}%>

</table>
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Stevie Adams:

<table>

<%for(int i=0;i<=count;i++){%>
<tr><td>Phone Numbers</td></tr>
<TD><%= address.getPhonenumber() %>
</TD>
<%}%>

</table>


you have the <td> without <tr>. try this.

<table>
<%for(int i=0;i<=count;i++){%>
<tr>
<td>Phone Numbers</td>
<td><%= address.getPhonenumber()%></td>
</tr>
<%}%>
</table>
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

If you're working with a JDBC ResultSet the code would look like this:
(Assuming you've named your ResultSet 'rs'
and you have two columns named 'name' and 'phone'
in your table.)


The 'next' method increments the row pointer and returns either true or false depending on whether there are more rows in the resultSet or not.
[ December 26, 2004: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 29249
    
139

Stevie,
Ben's example is a good one. If you copy/paste it, note that the open bracket is missing after the while loop.

Also, note that it is considered bad practice to put too much code in a JSP, especially database code. It's fine when learning, but it's something to be aware of. Long term, you want to do the "real work" in a servlet, put the results in an attribute in the request, forward to a JSP and use tags to display the result.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

And if you really want to be considered one of the cool kids on the block, forgoe scriplets for the loop in favor of the JSTL.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Good catch, Jean.
Thank you.

PS: I've fixed the offending line in the post.
You may or may not want to leave the correction up there.
[ December 26, 2004: Message edited by: Ben Souther ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP creating html rows for displaying data
 
Similar Threads
sorting selected checkboxes to the top of table
how to group the radio buttons in jsf data table
Should I use objects or not???
Need help with Array (setting variable)
Avoiding java code generate HTML pages