You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Richard Hill <RH...@classmates.com> on 2003/07/15 01:36:17 UTC

Iterating the resultset contents in the view (jsp)

Hi,
I'm working on an action that gets a resultset from a database table
containing 4 columns. I need to pass that information back to the view (jsp)
which will iterate over results. My question is what is the best way to do
this. Do I create an array for each row in the resultset and insert each
array in a collection, passing that back to the view? 

If so, how would you iterate over each array in the collection with the
logic:iterate taglib? All of the examples only show iterations over single
column lists.

Any help would be appreciated.

Thanks,
Richard


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Mon, 14 Jul 2003, Richard Hill wrote:

> Date: Mon, 14 Jul 2003 16:36:17 -0700
> From: Richard Hill <RH...@classmates.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: "'struts-user@jakarta.apache.org'" <st...@jakarta.apache.org>
> Subject: Iterating the resultset contents in the view (jsp)
>
> Hi,
> I'm working on an action that gets a resultset from a database table
> containing 4 columns. I need to pass that information back to the view (jsp)
> which will iterate over results. My question is what is the best way to do
> this. Do I create an array for each row in the resultset and insert each
> array in a collection, passing that back to the view?
>

That is certainly one approach.  Indeed, commons-beanutils has a useful
little class (org.apache.commons.beanutils.RowSetDynaClass) that is
ideally suited to this use case.  It creates a List of DynaBeans
representing the data content returned by the SELECT.  Because it makes a
copy, you can close the result set (and return the connection back to the
connection pool) before forwarding to the page.

> If so, how would you iterate over each array in the collection with the
> logic:iterate taglib? All of the examples only show iterations over single
> column lists.
>

Let's assume you have done this in your Action:

  ResultSet rs = ...;
  RowSetDynaClass rsdc = new RowSetDynaClass(rs);
  rs.close();
  request.setAttribute("customers", rsdc.getList());

so you now have a request attribute containing the list.  Now, in your
page, you can say things like:

  <logic:iterate id="customer" name="customers">
    Name is <bean:write name="customer" property="name"/>
    Status is <bean:write name="customer" property="status"/>
  </logic:iterate>

and so on.  Details of RowSetDynaClass are in the javadocs for BeanUtils:

  http://jakarta.apache.org/commons/beanutils/

> Any help would be appreciated.
>
> Thanks,
> Richard
>

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by David Graham <gr...@yahoo.com>.
--- "Craig R. McClanahan" <cr...@apache.org> wrote:
> 
> 
> On Tue, 15 Jul 2003, Kris Schneider wrote:
> 
> > Date: Tue, 15 Jul 2003 12:42:36 -0400
> > From: Kris Schneider <kr...@dotech.com>
> > Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> > To: Struts Users Mailing List <st...@jakarta.apache.org>
> > Subject: Re: Iterating the resultset contents in the view (jsp)
> >
> > Sure, understood. In fact I do this myself. But the reality is it's
> still an
> > identifier that's defined in the data layer, in this case embedded in
> SQL.
> 
> Isn't that going to be true even if you bury SQL inside a DAO?  Someone
> somewhere is going to have to deal with the column names that are
> defined
> in your database.
> 
> > My point was that I'd be more concerned with identifiers defined in
> the
> > data layer getting referenced in the view layer then I would with
> using
> > Result and ResultSupport outside of a web app.
> 
> A lot of people externalize SQL command strings into properties files so
> that they can tweak them for differences between databases.  This would
> also give you a level of separation, in that you can reference the
> actual
> column names ONLY in the properties file strigns, and have your code
> using
> the result set deal only with the aliases set with "as".

I've found this approach to be quite useful.  It gets tough when you need
to dynamically create a query based on a user's search form values.  In
that situation I define each SQL search chunk under it's own key and use
MessageFormat to insert values appropriately.

David

> 
> Craig
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 15 Jul 2003, Kris Schneider wrote:

> Date: Tue, 15 Jul 2003 12:42:36 -0400
> From: Kris Schneider <kr...@dotech.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts Users Mailing List <st...@jakarta.apache.org>
> Subject: Re: Iterating the resultset contents in the view (jsp)
>
> Sure, understood. In fact I do this myself. But the reality is it's still an
> identifier that's defined in the data layer, in this case embedded in SQL.

Isn't that going to be true even if you bury SQL inside a DAO?  Someone
somewhere is going to have to deal with the column names that are defined
in your database.

> My point was that I'd be more concerned with identifiers defined in the
> data layer getting referenced in the view layer then I would with using
> Result and ResultSupport outside of a web app.

A lot of people externalize SQL command strings into properties files so
that they can tweak them for differences between databases.  This would
also give you a level of separation, in that you can reference the actual
column names ONLY in the properties file strigns, and have your code using
the result set deal only with the aliases set with "as".

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by Kris Schneider <kr...@dotech.com>.
Sure, understood. In fact I do this myself. But the reality is it's still an
identifier that's defined in the data layer, in this case embedded in SQL. My
point was that I'd be more concerned with identifiers defined in the data layer
getting referenced in the view layer then I would with using Result and
ResultSupport outside of a web app.

P.S.
After an initial test, using Result and ResultSupport outside of a web app
appears to work just fine (only requires jstl.jar). FWIW...

Quoting "Craig R. McClanahan" <cr...@apache.org>:

> 
> 
> On Tue, 15 Jul 2003, Kris Schneider wrote:
> 
> > Well, okay, that makes the discussion a bit more abstract ;-). I
> > understand your point, but the bottom line is that it's just a namespace
> > (package) and an external library (JAR file). To my mind, the real
> > question with either Result or RowSetDynaClass is how far from the data
> > layer do you want the column names to propagate? In some cases that may
> > be all the way to the view layer.
> 
> Column names need propogate only as far as the SQL query, if that's what
> you want, thanks to the "as" capability of SQL:
> 
>   select customer_name as name, account_id as accountId
>     from customers
>     where past_due_status = 'Bad Boy';
> 
> the column names in the result set will be "name" and "accountId", instead
> of whatever they are inside the database.
> 
> Craig

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by "Craig R. McClanahan" <cr...@apache.org>.

On Tue, 15 Jul 2003, Kris Schneider wrote:

> Well, okay, that makes the discussion a bit more abstract ;-). I
> understand your point, but the bottom line is that it's just a namespace
> (package) and an external library (JAR file). To my mind, the real
> question with either Result or RowSetDynaClass is how far from the data
> layer do you want the column names to propagate? In some cases that may
> be all the way to the view layer.

Column names need propogate only as far as the SQL query, if that's what
you want, thanks to the "as" capability of SQL:

  select customer_name as name, account_id as accountId
    from customers
    where past_due_status = 'Bad Boy';

the column names in the result set will be "name" and "accountId", instead
of whatever they are inside the database.

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by David Graham <gr...@yahoo.com>.
--- Kris Schneider <kr...@dotech.com> wrote:
> Well, okay, that makes the discussion a bit more abstract ;-). I
> understand your
> point, but the bottom line is that it's just a namespace (package) and
> an
> external library (JAR file). To my mind, the real question with either
> Result or
> RowSetDynaClass is how far from the data layer do you want the column
> names to
> propagate? In some cases that may be all the way to the view layer.

This really has nothing to do with database column names because you can
use the SQL "AS" keyword to alias the names to anything you like.

David



> 
> Quoting David Graham <gr...@yahoo.com>:
> 
> > --- Kris Schneider <kr...@dotech.com> wrote:
> > > I'll admit that using a package called javax.servlet.jsp.jstl.sql in
> a 
> > > non-Web app is a bit odd, but I'm not seeing any dependencies on
> other 
> > > javax.servlet classes or interfaces or a Web container. I haven't
> tried 
> > > it, but I'd bet the examples below would work outside a Web app. 
> > 
> > Maybe, but it just feels wrong.  I know I've made a mistake when
> something
> > in the app doesn't feel right.
> > 
> > David
> > 
> > > Worth a try sometime...
> > > 
> > > David Graham wrote:
> > > > That's certainly another option but keep in mind that it ties your
> app
> > > to
> > > > the web.  That may be ok if you don't plan on reusing any of the
> code
> > > from
> > > > the app but DynaBeans are more appropriate for building layers
> that
> > > are
> > > > reusable in any environment.
> > > > 
> > > > David
> > > > 
> > > > --- Kris Schneider <kr...@dotech.com> wrote:
> > > > 
> > > >>Another datapoint: JSTL provides an interface called 
> > > >>javax.servlet.jsp.jstl.sql.Result:
> > > >>
> > > >>public interface Result {
> > > >>   public SortedMap[] getRows();
> > > >>   public Object[][]  getRowsByIndex();
> > > >>   public String[]    getColumnNames();
> > > >>   public int         getRowCount();
> > > >>   public boolean     isLimitedByMaxRows();
> > > >>}
> > > >>
> > > >>and a utility class javax.servlet.jsp.jstl.sql.ResultSupport:
> > > >>
> > > >>public class ResultSupport {
> > > >>   public static Result toResult(ResultSet rs);
> > > >>   public static Result toResult(ResultSet rs, int maxRows);
> > > >>}
> > > >>
> > > >>to accomplish something similar. The SortedMap instances returned
> from
> > > 
> > > >>Result.getRows are keyed by the ResultSet's column names and use
> the 
> > > >>Comparator String.CASE_INSENSITIVE_ORDER so that you don't have to
> > > worry
> > > >>
> > > >>about matching the case of the column name exactly. So, depending
> on 
> > > >>what your preference is, you can just do:
> > > >>
> > > >>ResultSet rs = ...;
> > > >>Result result = ResultSupport.toResult(rs);
> > > >>request.setAttribute(MyConstants.RESULT, result);
> > > >>
> > > >>or:
> > > >>
> > > >>ResultSet rs = ...;
> > > >>Result result = ResultSupport.toResult(rs);
> > > >>Map[] rows = result.getRows();
> > > >>request.setAttribute(MyConstants.ROWS, rows);
> > > >>
> > > >>David Graham wrote:
> > > >>
> > > >>>--- Richard Hill <RH...@classmates.com> wrote:
> > > >>>
> > > >>>
> > > >>>>Hi,
> > > >>>>I'm working on an action that gets a resultset from a database
> table
> > > >>>>containing 4 columns. I need to pass that information back to
> the
> > > view
> > > >>>>(jsp)
> > > >>>>which will iterate over results. My question is what is the best
> way
> > > >>
> > > >>to
> > > >>
> > > >>>>do
> > > >>>>this. Do I create an array for each row in the resultset and
> insert
> > > >>
> > > >>each
> > > >>
> > > >>>>array in a collection, passing that back to the view? 
> > > >>>
> > > >>>
> > > >>>A fairly standard approach is to create a class that represents a
> row
> > > >>
> > > >>of
> > > >>
> > > >>>your ResultSet and store a List of those objects in the request
> for
> > > >>
> > > >>the
> > > >>
> > > >>>page to iterate over.  
> > > >>>
> > > >>>If you don't want to create a class for each result, you should
> check
> > > >>
> > > >>out
> > > >>
> > > >>>the BeanUtils DynaBeans.  This little gem:
> > > >>>
> > > >>
> > > >
> > >
> >
>
http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/RowSetDynaClass.html
> > > > 
> > > >>>allows you to transer ResultSet data to DynaBeans in a trivial
> amount
> > > >>
> > > >>of
> > > >>
> > > >>>code.
> > > >>>
> > > >>>David
> > > >>>
> > > >>>
> > > >>>
> > > >>>
> > > >>>>If so, how would you iterate over each array in the collection
> with
> > > >>
> > > >>the
> > > >>
> > > >>>>logic:iterate taglib? All of the examples only show iterations
> over
> > > >>>>single
> > > >>>>column lists.
> > > >>>>
> > > >>>>Any help would be appreciated.
> > > >>>>
> > > >>>>Thanks,
> > > >>>>Richard
> > > >>
> > > >>-- 
> > > >>Kris Schneider <ma...@dotech.com>
> > > >>D.O.Tech       <http://www.dotech.com/>
> > > 
> > > -- 
> > > Kris Schneider <ma...@dotech.com>
> > > D.O.Tech       <http://www.dotech.com/>
> 
> -- 
> Kris Schneider <ma...@dotech.com>
> D.O.Tech       <http://www.dotech.com/>


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by Kris Schneider <kr...@dotech.com>.
Well, okay, that makes the discussion a bit more abstract ;-). I understand your
point, but the bottom line is that it's just a namespace (package) and an
external library (JAR file). To my mind, the real question with either Result or
RowSetDynaClass is how far from the data layer do you want the column names to
propagate? In some cases that may be all the way to the view layer.

Quoting David Graham <gr...@yahoo.com>:

> --- Kris Schneider <kr...@dotech.com> wrote:
> > I'll admit that using a package called javax.servlet.jsp.jstl.sql in a 
> > non-Web app is a bit odd, but I'm not seeing any dependencies on other 
> > javax.servlet classes or interfaces or a Web container. I haven't tried 
> > it, but I'd bet the examples below would work outside a Web app. 
> 
> Maybe, but it just feels wrong.  I know I've made a mistake when something
> in the app doesn't feel right.
> 
> David
> 
> > Worth a try sometime...
> > 
> > David Graham wrote:
> > > That's certainly another option but keep in mind that it ties your app
> > to
> > > the web.  That may be ok if you don't plan on reusing any of the code
> > from
> > > the app but DynaBeans are more appropriate for building layers that
> > are
> > > reusable in any environment.
> > > 
> > > David
> > > 
> > > --- Kris Schneider <kr...@dotech.com> wrote:
> > > 
> > >>Another datapoint: JSTL provides an interface called 
> > >>javax.servlet.jsp.jstl.sql.Result:
> > >>
> > >>public interface Result {
> > >>   public SortedMap[] getRows();
> > >>   public Object[][]  getRowsByIndex();
> > >>   public String[]    getColumnNames();
> > >>   public int         getRowCount();
> > >>   public boolean     isLimitedByMaxRows();
> > >>}
> > >>
> > >>and a utility class javax.servlet.jsp.jstl.sql.ResultSupport:
> > >>
> > >>public class ResultSupport {
> > >>   public static Result toResult(ResultSet rs);
> > >>   public static Result toResult(ResultSet rs, int maxRows);
> > >>}
> > >>
> > >>to accomplish something similar. The SortedMap instances returned from
> > 
> > >>Result.getRows are keyed by the ResultSet's column names and use the 
> > >>Comparator String.CASE_INSENSITIVE_ORDER so that you don't have to
> > worry
> > >>
> > >>about matching the case of the column name exactly. So, depending on 
> > >>what your preference is, you can just do:
> > >>
> > >>ResultSet rs = ...;
> > >>Result result = ResultSupport.toResult(rs);
> > >>request.setAttribute(MyConstants.RESULT, result);
> > >>
> > >>or:
> > >>
> > >>ResultSet rs = ...;
> > >>Result result = ResultSupport.toResult(rs);
> > >>Map[] rows = result.getRows();
> > >>request.setAttribute(MyConstants.ROWS, rows);
> > >>
> > >>David Graham wrote:
> > >>
> > >>>--- Richard Hill <RH...@classmates.com> wrote:
> > >>>
> > >>>
> > >>>>Hi,
> > >>>>I'm working on an action that gets a resultset from a database table
> > >>>>containing 4 columns. I need to pass that information back to the
> > view
> > >>>>(jsp)
> > >>>>which will iterate over results. My question is what is the best way
> > >>
> > >>to
> > >>
> > >>>>do
> > >>>>this. Do I create an array for each row in the resultset and insert
> > >>
> > >>each
> > >>
> > >>>>array in a collection, passing that back to the view? 
> > >>>
> > >>>
> > >>>A fairly standard approach is to create a class that represents a row
> > >>
> > >>of
> > >>
> > >>>your ResultSet and store a List of those objects in the request for
> > >>
> > >>the
> > >>
> > >>>page to iterate over.  
> > >>>
> > >>>If you don't want to create a class for each result, you should check
> > >>
> > >>out
> > >>
> > >>>the BeanUtils DynaBeans.  This little gem:
> > >>>
> > >>
> > >
> >
>
http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/RowSetDynaClass.html
> > > 
> > >>>allows you to transer ResultSet data to DynaBeans in a trivial amount
> > >>
> > >>of
> > >>
> > >>>code.
> > >>>
> > >>>David
> > >>>
> > >>>
> > >>>
> > >>>
> > >>>>If so, how would you iterate over each array in the collection with
> > >>
> > >>the
> > >>
> > >>>>logic:iterate taglib? All of the examples only show iterations over
> > >>>>single
> > >>>>column lists.
> > >>>>
> > >>>>Any help would be appreciated.
> > >>>>
> > >>>>Thanks,
> > >>>>Richard
> > >>
> > >>-- 
> > >>Kris Schneider <ma...@dotech.com>
> > >>D.O.Tech       <http://www.dotech.com/>
> > 
> > -- 
> > Kris Schneider <ma...@dotech.com>
> > D.O.Tech       <http://www.dotech.com/>

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by David Graham <gr...@yahoo.com>.
--- Kris Schneider <kr...@dotech.com> wrote:
> I'll admit that using a package called javax.servlet.jsp.jstl.sql in a 
> non-Web app is a bit odd, but I'm not seeing any dependencies on other 
> javax.servlet classes or interfaces or a Web container. I haven't tried 
> it, but I'd bet the examples below would work outside a Web app. 

Maybe, but it just feels wrong.  I know I've made a mistake when something
in the app doesn't feel right.

David

> Worth a try sometime...
> 
> David Graham wrote:
> > That's certainly another option but keep in mind that it ties your app
> to
> > the web.  That may be ok if you don't plan on reusing any of the code
> from
> > the app but DynaBeans are more appropriate for building layers that
> are
> > reusable in any environment.
> > 
> > David
> > 
> > --- Kris Schneider <kr...@dotech.com> wrote:
> > 
> >>Another datapoint: JSTL provides an interface called 
> >>javax.servlet.jsp.jstl.sql.Result:
> >>
> >>public interface Result {
> >>   public SortedMap[] getRows();
> >>   public Object[][]  getRowsByIndex();
> >>   public String[]    getColumnNames();
> >>   public int         getRowCount();
> >>   public boolean     isLimitedByMaxRows();
> >>}
> >>
> >>and a utility class javax.servlet.jsp.jstl.sql.ResultSupport:
> >>
> >>public class ResultSupport {
> >>   public static Result toResult(ResultSet rs);
> >>   public static Result toResult(ResultSet rs, int maxRows);
> >>}
> >>
> >>to accomplish something similar. The SortedMap instances returned from
> 
> >>Result.getRows are keyed by the ResultSet's column names and use the 
> >>Comparator String.CASE_INSENSITIVE_ORDER so that you don't have to
> worry
> >>
> >>about matching the case of the column name exactly. So, depending on 
> >>what your preference is, you can just do:
> >>
> >>ResultSet rs = ...;
> >>Result result = ResultSupport.toResult(rs);
> >>request.setAttribute(MyConstants.RESULT, result);
> >>
> >>or:
> >>
> >>ResultSet rs = ...;
> >>Result result = ResultSupport.toResult(rs);
> >>Map[] rows = result.getRows();
> >>request.setAttribute(MyConstants.ROWS, rows);
> >>
> >>David Graham wrote:
> >>
> >>>--- Richard Hill <RH...@classmates.com> wrote:
> >>>
> >>>
> >>>>Hi,
> >>>>I'm working on an action that gets a resultset from a database table
> >>>>containing 4 columns. I need to pass that information back to the
> view
> >>>>(jsp)
> >>>>which will iterate over results. My question is what is the best way
> >>
> >>to
> >>
> >>>>do
> >>>>this. Do I create an array for each row in the resultset and insert
> >>
> >>each
> >>
> >>>>array in a collection, passing that back to the view? 
> >>>
> >>>
> >>>A fairly standard approach is to create a class that represents a row
> >>
> >>of
> >>
> >>>your ResultSet and store a List of those objects in the request for
> >>
> >>the
> >>
> >>>page to iterate over.  
> >>>
> >>>If you don't want to create a class for each result, you should check
> >>
> >>out
> >>
> >>>the BeanUtils DynaBeans.  This little gem:
> >>>
> >>
> >
>
http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/RowSetDynaClass.html
> > 
> >>>allows you to transer ResultSet data to DynaBeans in a trivial amount
> >>
> >>of
> >>
> >>>code.
> >>>
> >>>David
> >>>
> >>>
> >>>
> >>>
> >>>>If so, how would you iterate over each array in the collection with
> >>
> >>the
> >>
> >>>>logic:iterate taglib? All of the examples only show iterations over
> >>>>single
> >>>>column lists.
> >>>>
> >>>>Any help would be appreciated.
> >>>>
> >>>>Thanks,
> >>>>Richard
> >>
> >>-- 
> >>Kris Schneider <ma...@dotech.com>
> >>D.O.Tech       <http://www.dotech.com/>
> 
> -- 
> Kris Schneider <ma...@dotech.com>
> D.O.Tech       <http://www.dotech.com/>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by Kris Schneider <kr...@dotech.com>.
I'll admit that using a package called javax.servlet.jsp.jstl.sql in a 
non-Web app is a bit odd, but I'm not seeing any dependencies on other 
javax.servlet classes or interfaces or a Web container. I haven't tried 
it, but I'd bet the examples below would work outside a Web app. Worth a 
try sometime...

David Graham wrote:
> That's certainly another option but keep in mind that it ties your app to
> the web.  That may be ok if you don't plan on reusing any of the code from
> the app but DynaBeans are more appropriate for building layers that are
> reusable in any environment.
> 
> David
> 
> --- Kris Schneider <kr...@dotech.com> wrote:
> 
>>Another datapoint: JSTL provides an interface called 
>>javax.servlet.jsp.jstl.sql.Result:
>>
>>public interface Result {
>>   public SortedMap[] getRows();
>>   public Object[][]  getRowsByIndex();
>>   public String[]    getColumnNames();
>>   public int         getRowCount();
>>   public boolean     isLimitedByMaxRows();
>>}
>>
>>and a utility class javax.servlet.jsp.jstl.sql.ResultSupport:
>>
>>public class ResultSupport {
>>   public static Result toResult(ResultSet rs);
>>   public static Result toResult(ResultSet rs, int maxRows);
>>}
>>
>>to accomplish something similar. The SortedMap instances returned from 
>>Result.getRows are keyed by the ResultSet's column names and use the 
>>Comparator String.CASE_INSENSITIVE_ORDER so that you don't have to worry
>>
>>about matching the case of the column name exactly. So, depending on 
>>what your preference is, you can just do:
>>
>>ResultSet rs = ...;
>>Result result = ResultSupport.toResult(rs);
>>request.setAttribute(MyConstants.RESULT, result);
>>
>>or:
>>
>>ResultSet rs = ...;
>>Result result = ResultSupport.toResult(rs);
>>Map[] rows = result.getRows();
>>request.setAttribute(MyConstants.ROWS, rows);
>>
>>David Graham wrote:
>>
>>>--- Richard Hill <RH...@classmates.com> wrote:
>>>
>>>
>>>>Hi,
>>>>I'm working on an action that gets a resultset from a database table
>>>>containing 4 columns. I need to pass that information back to the view
>>>>(jsp)
>>>>which will iterate over results. My question is what is the best way
>>
>>to
>>
>>>>do
>>>>this. Do I create an array for each row in the resultset and insert
>>
>>each
>>
>>>>array in a collection, passing that back to the view? 
>>>
>>>
>>>A fairly standard approach is to create a class that represents a row
>>
>>of
>>
>>>your ResultSet and store a List of those objects in the request for
>>
>>the
>>
>>>page to iterate over.  
>>>
>>>If you don't want to create a class for each result, you should check
>>
>>out
>>
>>>the BeanUtils DynaBeans.  This little gem:
>>>
>>
> http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/RowSetDynaClass.html
> 
>>>allows you to transer ResultSet data to DynaBeans in a trivial amount
>>
>>of
>>
>>>code.
>>>
>>>David
>>>
>>>
>>>
>>>
>>>>If so, how would you iterate over each array in the collection with
>>
>>the
>>
>>>>logic:iterate taglib? All of the examples only show iterations over
>>>>single
>>>>column lists.
>>>>
>>>>Any help would be appreciated.
>>>>
>>>>Thanks,
>>>>Richard
>>
>>-- 
>>Kris Schneider <ma...@dotech.com>
>>D.O.Tech       <http://www.dotech.com/>

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by David Graham <gr...@yahoo.com>.
That's certainly another option but keep in mind that it ties your app to
the web.  That may be ok if you don't plan on reusing any of the code from
the app but DynaBeans are more appropriate for building layers that are
reusable in any environment.

David

--- Kris Schneider <kr...@dotech.com> wrote:
> Another datapoint: JSTL provides an interface called 
> javax.servlet.jsp.jstl.sql.Result:
> 
> public interface Result {
>    public SortedMap[] getRows();
>    public Object[][]  getRowsByIndex();
>    public String[]    getColumnNames();
>    public int         getRowCount();
>    public boolean     isLimitedByMaxRows();
> }
> 
> and a utility class javax.servlet.jsp.jstl.sql.ResultSupport:
> 
> public class ResultSupport {
>    public static Result toResult(ResultSet rs);
>    public static Result toResult(ResultSet rs, int maxRows);
> }
> 
> to accomplish something similar. The SortedMap instances returned from 
> Result.getRows are keyed by the ResultSet's column names and use the 
> Comparator String.CASE_INSENSITIVE_ORDER so that you don't have to worry
> 
> about matching the case of the column name exactly. So, depending on 
> what your preference is, you can just do:
> 
> ResultSet rs = ...;
> Result result = ResultSupport.toResult(rs);
> request.setAttribute(MyConstants.RESULT, result);
> 
> or:
> 
> ResultSet rs = ...;
> Result result = ResultSupport.toResult(rs);
> Map[] rows = result.getRows();
> request.setAttribute(MyConstants.ROWS, rows);
> 
> David Graham wrote:
> > --- Richard Hill <RH...@classmates.com> wrote:
> > 
> >>Hi,
> >>I'm working on an action that gets a resultset from a database table
> >>containing 4 columns. I need to pass that information back to the view
> >>(jsp)
> >>which will iterate over results. My question is what is the best way
> to
> >>do
> >>this. Do I create an array for each row in the resultset and insert
> each
> >>array in a collection, passing that back to the view? 
> > 
> > 
> > A fairly standard approach is to create a class that represents a row
> of
> > your ResultSet and store a List of those objects in the request for
> the
> > page to iterate over.  
> > 
> > If you don't want to create a class for each result, you should check
> out
> > the BeanUtils DynaBeans.  This little gem:
> >
>
http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/RowSetDynaClass.html
> > allows you to transer ResultSet data to DynaBeans in a trivial amount
> of
> > code.
> > 
> > David
> > 
> > 
> > 
> >>If so, how would you iterate over each array in the collection with
> the
> >>logic:iterate taglib? All of the examples only show iterations over
> >>single
> >>column lists.
> >>
> >>Any help would be appreciated.
> >>
> >>Thanks,
> >>Richard
> 
> -- 
> Kris Schneider <ma...@dotech.com>
> D.O.Tech       <http://www.dotech.com/>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by Kris Schneider <kr...@dotech.com>.
Another datapoint: JSTL provides an interface called 
javax.servlet.jsp.jstl.sql.Result:

public interface Result {
   public SortedMap[] getRows();
   public Object[][]  getRowsByIndex();
   public String[]    getColumnNames();
   public int         getRowCount();
   public boolean     isLimitedByMaxRows();
}

and a utility class javax.servlet.jsp.jstl.sql.ResultSupport:

public class ResultSupport {
   public static Result toResult(ResultSet rs);
   public static Result toResult(ResultSet rs, int maxRows);
}

to accomplish something similar. The SortedMap instances returned from 
Result.getRows are keyed by the ResultSet's column names and use the 
Comparator String.CASE_INSENSITIVE_ORDER so that you don't have to worry 
about matching the case of the column name exactly. So, depending on 
what your preference is, you can just do:

ResultSet rs = ...;
Result result = ResultSupport.toResult(rs);
request.setAttribute(MyConstants.RESULT, result);

or:

ResultSet rs = ...;
Result result = ResultSupport.toResult(rs);
Map[] rows = result.getRows();
request.setAttribute(MyConstants.ROWS, rows);

David Graham wrote:
> --- Richard Hill <RH...@classmates.com> wrote:
> 
>>Hi,
>>I'm working on an action that gets a resultset from a database table
>>containing 4 columns. I need to pass that information back to the view
>>(jsp)
>>which will iterate over results. My question is what is the best way to
>>do
>>this. Do I create an array for each row in the resultset and insert each
>>array in a collection, passing that back to the view? 
> 
> 
> A fairly standard approach is to create a class that represents a row of
> your ResultSet and store a List of those objects in the request for the
> page to iterate over.  
> 
> If you don't want to create a class for each result, you should check out
> the BeanUtils DynaBeans.  This little gem:
> http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/RowSetDynaClass.html
> allows you to transer ResultSet data to DynaBeans in a trivial amount of
> code.
> 
> David
> 
> 
> 
>>If so, how would you iterate over each array in the collection with the
>>logic:iterate taglib? All of the examples only show iterations over
>>single
>>column lists.
>>
>>Any help would be appreciated.
>>
>>Thanks,
>>Richard

-- 
Kris Schneider <ma...@dotech.com>
D.O.Tech       <http://www.dotech.com/>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: Iterating the resultset contents in the view (jsp)

Posted by David Graham <gr...@yahoo.com>.
--- Richard Hill <RH...@classmates.com> wrote:
> Hi,
> I'm working on an action that gets a resultset from a database table
> containing 4 columns. I need to pass that information back to the view
> (jsp)
> which will iterate over results. My question is what is the best way to
> do
> this. Do I create an array for each row in the resultset and insert each
> array in a collection, passing that back to the view? 

A fairly standard approach is to create a class that represents a row of
your ResultSet and store a List of those objects in the request for the
page to iterate over.  

If you don't want to create a class for each result, you should check out
the BeanUtils DynaBeans.  This little gem:
http://jakarta.apache.org/commons/beanutils/api/org/apache/commons/beanutils/RowSetDynaClass.html
allows you to transer ResultSet data to DynaBeans in a trivial amount of
code.

David


> 
> If so, how would you iterate over each array in the collection with the
> logic:iterate taglib? All of the examples only show iterations over
> single
> column lists.
> 
> Any help would be appreciated.
> 
> Thanks,
> Richard
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org