• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSP creating html rows for displaying data

 
Stevie Adams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34202
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Stevie Adams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34202
341
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64715
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic