Just adding to what Shawn said, you may also want to try a joined
query instead of repeatedly accessing the database. It should be more
efficient in most databases. With the JSTL actions, you can then
take care of the display details when you render the table instead:

   <sql:query var="addrs">
     SELECT * FROM Names, Addresses, Phone
     WHERE Names.Id = Addresses.Name_Id AND
       Addresses.Id = Phone.Address_id
     ORDER BY Names.Id, Addressed.Id
   </sql:query>

   <c:forEach items="$addrs.rows" var="current">
     <c:if test="${name != current['Names.Id']}">
       <c:set var="name" value="$current['Names.Id']}" />
       Name: <c:out value="${current.name}" />
     </c:if>
     <c:if test="${address != current['Addresses.Id']}">
       <c:set var="address" value="$current['Addresses.Id']}" />
       Address: <c:out value="${current.address}" />
     </c:if>
     Phone: <c:out value="${current.phone}" />
   </c:forEach>

Note that you may have to use an explicit column list in the SELECT
list instead of "*" and assign aliases to the Id columns in all tables,
and then adjust the names used in the rest of the example accordingly.
The syntax for aliases varies a bit between databases but many support
something like

   SELECT Names.Id AS Names_Id, Addresses.Id AS Addresses_Id ...

Hans

Hughes, Matt wrote:
> Ok basically I want to do something really simple but I'm not exactly sure
> how to do it with tag libraries.  I have three tables: name, address, phone.
> Each name can have multiple addresses, and each address can have multiple
> phones.  So using plain old jsp it would look something like:
> 
> rs1 = "select * from names";
> while (rs1.next()) {
>     rs2 = "select * from addresses where name_id = names.id";
>     while(rs2.next()) {
>           rs3 = "select * from phone where address_id = addresses.id";
>           while (rs3.next()) {
>           }
>     }
> }
> 
> So that the output would look something like:
> 
> Matt Hughes
>     Address 1
>         Phone 1
>         Phone 2
>     Address 2
> etc...etc...
> 
> Yeah, it's really very pseudocode, but I think it's understandable.  So I've
> created three generic tags: connection, resultset, and column.  How do I
> recreate those kinds of nested while loops using tags.  I almost want to put
> the beginning of the while loop in the doStartTag and the end bracket in the
> doEndTag...if only it were that easy.  Any suggestions on how I could make
> the tags work to something like below:
> 
> <Connection id="conn1">
>    <resultset conn="conn1" id="rs1">
>        <column rs="rs1">first_name</column>
>        <resultset conn="conn1" id="rs2">
>            <column rs="rs2">address_1</column>
>            <resultset conn="conn1" id="rs3">
>                 <column rs="rs3">phone_1</column>
>            </resultset>
>         </resultset>
>     </resultset>
> </connection>
> 
> 
> The other problem I'm having with this is how to pass the field_id from the
> second table into the third tag.  Ideally it should look something like:
> 
> ...
>               <resultset conn="conn1" id="rs3" QueryParameter="<resultset
> conn="conn1" id="rs2">field_id</resultset>
>                       <column rs="rs3">contact name</column>
>               </resultset>
> 
> ------------------------------------------------------------------------------
> Notice: This e-mail message, together with any attachments, contains information of 
>Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, 
>proprietary copyrighted and/or legally privileged, and is intended solely for the use 
>of the individual or entity named on this message. If you are not the intended 
>recipient, and have received this message in error, please immediately return this by 
>e-mail and then delete it.
> 
> ==============================================================================
> 
> 
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> 


-- 
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
JavaServer Pages        http://TheJSPBook.com


--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to