You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Marco Pas <ma...@cmg.nl> on 2001/11/20 17:25:53 UTC

Howto: Velocity and database iterations ?

How should you i implement database / row iterations using Velocity ?

- Marco

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Howto: Velocity and database iterations ?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/21/01 7:22 AM, "Rickard Öberg" <ri...@xpedio.com> wrote:

> Geir Magnusson Jr. wrote:
> 
>> ArrayList data = new ArrayList();
>> 
>> while(rs.next())
>> {
>>     HashMap hm = new HashMap();
>> 
>>     hm.put("name", rs.getString("user_name") );
>>     hm.put("address", rs.getString("home_address") );
>> 
>>     data.add( hm );
>> }
>> 
>> context.put("list", data );
> 
> 
> It'd be much more efficient if you wrapped the resultset in a class that
> looks like a java.util.List, and returns objects that are Map wrappers
> that access the rows. The Map wrapper could be reused for all rows, so
> there's very little memory/CPU overhead for using this technique.

True.  The only issue there is that you can't hang onto it in the template,
as the values change underneath you...

Needed something illustrative and simple to write as an example...  :)

-- 
Geir Magnusson Jr.                       geirm@optonline.net
System and Software Consulting
You're going to end up getting pissed at your software
anyway, so you might as well not pay for it. Try Open Source.



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Howto: Velocity and database iterations ?

Posted by Rickard Öberg <ri...@xpedio.com>.
Geir Magnusson Jr. wrote:

> ArrayList data = new ArrayList();
> 
> while(rs.next())
> {
>     HashMap hm = new HashMap();
> 
>     hm.put("name", rs.getString("user_name") );
>     hm.put("address", rs.getString("home_address") );
> 
>     data.add( hm );
> }
> 
> context.put("list", data );


It'd be much more efficient if you wrapped the resultset in a class that 
looks like a java.util.List, and returns objects that are Map wrappers 
that access the rows. The Map wrapper could be reused for all rows, so 
there's very little memory/CPU overhead for using this technique.


/Rickard



-- 
Rickard Öberg


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Howto: Velocity and database iterations ?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/20/01 11:25 AM, "Marco Pas" <ma...@cmg.nl> wrote:

> How should you i implement database / row iterations using Velocity ?
> 

People do it in all sorts of [strange] ways.

When things are small, I tend to do foolish things like construct little
vector/map structures for the data.  Modulo any real error checking...


// get a ResultSet

ArrayList data = new ArrayList();

while(rs.next())
{
    HashMap hm = new HashMap();

    hm.put("name", rs.getString("user_name") );
    hm.put("address", rs.getString("home_address") );

    data.add( hm );
}

context.put("list", data );


So in the template...


  <table>
     #foreach( $row in $list )
      <tr>
      <td>$row.name</td><td>$row.address</td>
      </tr>
     #end
  </table>


Of course, there are other ways for different applications.

But the above has served me well for small stuff.

You can even write a utility that uses the result set meta data to
automatically turn a result set into a array/map....

geir

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"He who throws mud only loses ground." - Fat Albert


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>