You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Barry Volpe <st...@childrencare.com> on 2003/11/03 20:35:56 UTC

DynaAction form and ArrayList


Only Last row is available in my ArrayList?

Have the following:

ArrayList teachers = new ArrayList();
DynaActionForm teacherForm = (DynaActionForm)form;

while ( rs.next() ) { 


System.out.println("firstname.rs: " + rs.getString("firstname"));

System.out.println("lastname.rs: " + rs.getString("lastname")); 

System.out.println("units.rs: " + rs.getString("units")); 





teacherForm.set("firstname",rs.getString("firstname"));

teacherForm.set("lastname",rs.getString("lastname")); 

teacherForm.set("units",rs.getString("units"));




System.out.println("firstname.get: " + teacherForm.get("firstname"));

System.out.println("lastname.get: " + teacherForm.get("lastname")); 

System.out.println("units.get: " + teacherForm.get("units")); 


teachers.add(teacherForm); 

}

The last row is stored in index 0 and 1.  I expect 0 to be the first row
and 1 to be the second row. 

System.out.println("teachers[0]: " + teachers.get(0));

System.out.println("teachers[1]: " + teachers.get(1)); 

Anyone know why?



Thanks,

Barry











Re: DynaAction form and ArrayList

Posted by Kris Schneider <kr...@dotech.com>.
That's just what RowSetDynaClass and the implementation of the Result interface
in JSTL do (use ResultSetMetaData to grab column names).

Quoting Mark Lowe <ma...@talk21.com>:

> It works alright but last time i played with these beanutil's toys it 
> was understandably slow. A little less slower is getting the 
> resultsetmetadata and using them as your property names.
> 
> IMO its better to make some beans and populate them with the result set 
> values, until you've time to put a proper model layer in place. At 
> least struts and the db stuff can be decoupled and the struts layer 
> reused.
> 
> 
> 
> On Monday, November 3, 2003, at 07:57 PM, Kris Schneider wrote:
> 
> > Per the JavaDoc for RowSetDynaClass:
> >
> > import org.apache.commons.beanutils.RowSetDynaClass;
> > ...
> > Connection conn = ...;  // Acquire connection from pool
> > Statement stmt = conn.createStatement();
> > ResultSet rs = stmt.executeQuery("SELECT ...");
> > RowSetDynaClass rsdc = new RowSetDynaClass(rs);
> > rs.close();
> > stmt.close();
> > ...;                    // Return connection to pool
> > List rows = rsdc.getRows();
> > ...;                   // Process the rows as desired
> >
> > JSTL also provides a similar solution with
> > javax.servlet.jsp.jstl.sql.ResultSupport and 
> > javax.servlet.jsp.jstl.sql.Result.
> >
> > Quoting Barry Volpe <st...@childrencare.com>:
> >
> >>
> >>
> >> Only Last row is available in my ArrayList?
> >>
> >> Have the following:
> >>
> >> ArrayList teachers = new ArrayList();
> >> DynaActionForm teacherForm = (DynaActionForm)form;
> >>
> >> while ( rs.next() ) {
> >>
> >>
> >> System.out.println("firstname.rs: " + rs.getString("firstname"));
> >>
> >> System.out.println("lastname.rs: " + rs.getString("lastname"));
> >>
> >> System.out.println("units.rs: " + rs.getString("units"));
> >>
> >>
> >>
> >>
> >>
> >> teacherForm.set("firstname",rs.getString("firstname"));
> >>
> >> teacherForm.set("lastname",rs.getString("lastname"));
> >>
> >> teacherForm.set("units",rs.getString("units"));
> >>
> >>
> >>
> >>
> >> System.out.println("firstname.get: " + teacherForm.get("firstname"));
> >>
> >> System.out.println("lastname.get: " + teacherForm.get("lastname"));
> >>
> >> System.out.println("units.get: " + teacherForm.get("units"));
> >>
> >>
> >> teachers.add(teacherForm);
> >>
> >> }
> >>
> >> The last row is stored in index 0 and 1.  I expect 0 to be the first 
> >> row
> >> and 1 to be the second row.
> >>
> >> System.out.println("teachers[0]: " + teachers.get(0));
> >>
> >> System.out.println("teachers[1]: " + teachers.get(1));
> >>
> >> Anyone know why?
> >>
> >>
> >>
> >> Thanks,
> >>
> >> Barry
> >
> > -- 
> > 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: DynaAction form and ArrayList

Posted by Mark Lowe <ma...@talk21.com>.
It works alright but last time i played with these beanutil's toys it 
was understandably slow. A little less slower is getting the 
resultsetmetadata and using them as your property names.

IMO its better to make some beans and populate them with the result set 
values, until you've time to put a proper model layer in place. At 
least struts and the db stuff can be decoupled and the struts layer 
reused.



On Monday, November 3, 2003, at 07:57 PM, Kris Schneider wrote:

> Per the JavaDoc for RowSetDynaClass:
>
> import org.apache.commons.beanutils.RowSetDynaClass;
> ...
> Connection conn = ...;  // Acquire connection from pool
> Statement stmt = conn.createStatement();
> ResultSet rs = stmt.executeQuery("SELECT ...");
> RowSetDynaClass rsdc = new RowSetDynaClass(rs);
> rs.close();
> stmt.close();
> ...;                    // Return connection to pool
> List rows = rsdc.getRows();
> ...;                   // Process the rows as desired
>
> JSTL also provides a similar solution with
> javax.servlet.jsp.jstl.sql.ResultSupport and 
> javax.servlet.jsp.jstl.sql.Result.
>
> Quoting Barry Volpe <st...@childrencare.com>:
>
>>
>>
>> Only Last row is available in my ArrayList?
>>
>> Have the following:
>>
>> ArrayList teachers = new ArrayList();
>> DynaActionForm teacherForm = (DynaActionForm)form;
>>
>> while ( rs.next() ) {
>>
>>
>> System.out.println("firstname.rs: " + rs.getString("firstname"));
>>
>> System.out.println("lastname.rs: " + rs.getString("lastname"));
>>
>> System.out.println("units.rs: " + rs.getString("units"));
>>
>>
>>
>>
>>
>> teacherForm.set("firstname",rs.getString("firstname"));
>>
>> teacherForm.set("lastname",rs.getString("lastname"));
>>
>> teacherForm.set("units",rs.getString("units"));
>>
>>
>>
>>
>> System.out.println("firstname.get: " + teacherForm.get("firstname"));
>>
>> System.out.println("lastname.get: " + teacherForm.get("lastname"));
>>
>> System.out.println("units.get: " + teacherForm.get("units"));
>>
>>
>> teachers.add(teacherForm);
>>
>> }
>>
>> The last row is stored in index 0 and 1.  I expect 0 to be the first 
>> row
>> and 1 to be the second row.
>>
>> System.out.println("teachers[0]: " + teachers.get(0));
>>
>> System.out.println("teachers[1]: " + teachers.get(1));
>>
>> Anyone know why?
>>
>>
>>
>> Thanks,
>>
>> Barry
>
> -- 
> 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
>


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


Re: DynaAction form and ArrayList

Posted by Kris Schneider <kr...@dotech.com>.
Per the JavaDoc for RowSetDynaClass:

import org.apache.commons.beanutils.RowSetDynaClass;
...
Connection conn = ...;  // Acquire connection from pool
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT ...");
RowSetDynaClass rsdc = new RowSetDynaClass(rs);
rs.close();
stmt.close();
...;                    // Return connection to pool
List rows = rsdc.getRows();
...;                   // Process the rows as desired

JSTL also provides a similar solution with
javax.servlet.jsp.jstl.sql.ResultSupport and javax.servlet.jsp.jstl.sql.Result.

Quoting Barry Volpe <st...@childrencare.com>:

> 
> 
> Only Last row is available in my ArrayList?
> 
> Have the following:
> 
> ArrayList teachers = new ArrayList();
> DynaActionForm teacherForm = (DynaActionForm)form;
> 
> while ( rs.next() ) { 
> 
> 
> System.out.println("firstname.rs: " + rs.getString("firstname"));
> 
> System.out.println("lastname.rs: " + rs.getString("lastname")); 
> 
> System.out.println("units.rs: " + rs.getString("units")); 
> 
> 
> 
> 
> 
> teacherForm.set("firstname",rs.getString("firstname"));
> 
> teacherForm.set("lastname",rs.getString("lastname")); 
> 
> teacherForm.set("units",rs.getString("units"));
> 
> 
> 
> 
> System.out.println("firstname.get: " + teacherForm.get("firstname"));
> 
> System.out.println("lastname.get: " + teacherForm.get("lastname")); 
> 
> System.out.println("units.get: " + teacherForm.get("units")); 
> 
> 
> teachers.add(teacherForm); 
> 
> }
> 
> The last row is stored in index 0 and 1.  I expect 0 to be the first row
> and 1 to be the second row. 
> 
> System.out.println("teachers[0]: " + teachers.get(0));
> 
> System.out.println("teachers[1]: " + teachers.get(1)); 
> 
> Anyone know why?
> 
> 
> 
> Thanks,
> 
> Barry

-- 
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: DynaAction form and ArrayList

Posted by Barry Volpe <st...@childrencare.com>.
Thanks Mark
Your suggestion worked. 

I did not use the 
teacherForm.set("teachers", teacherList);?

It looks like this is accomplished without using
DynaAction Form and using JSTL in my jsp page.

Barry


----- Original Message ----- 
From: "Mark Lowe" <ma...@talk21.com>
To: "Struts Users Mailing List" <st...@jakarta.apache.org>
Sent: Monday, November 03, 2003 11:42 AM
Subject: Re: DynaAction form and ArrayList


> 
> while(rs.next()) {
> Map teacher = new HashMap();
> teacher.put("firstName",rs.getString("firstName"));
> teacherList.add(teacher);
> }
> 
> teacherForm.set("teachers", teacherList);
> 
> request.setAttribute ("teachers", teacherList.toArray());
> 
> Cheers Mark
> 
> On Monday, November 3, 2003, at 07:35 PM, Barry Volpe wrote:
> 
> >
> >
> > Only Last row is available in my ArrayList?
> >
> > Have the following:
> >
> > ArrayList teachers = new ArrayList();
> > DynaActionForm teacherForm = (DynaActionForm)form;
> >
> > while ( rs.next() ) {
> >
> >
> > System.out.println("firstname.rs: " + rs.getString("firstname"));
> >
> > System.out.println("lastname.rs: " + rs.getString("lastname"));
> >
> > System.out.println("units.rs: " + rs.getString("units"));
> >
> >
> >
> >
> >
> > teacherForm.set("firstname",rs.getString("firstname"));
> >
> > teacherForm.set("lastname",rs.getString("lastname"));
> >
> > teacherForm.set("units",rs.getString("units"));
> >
> >
> >
> >
> > System.out.println("firstname.get: " + teacherForm.get("firstname"));
> >
> > System.out.println("lastname.get: " + teacherForm.get("lastname"));
> >
> > System.out.println("units.get: " + teacherForm.get("units"));
> >
> >
> > teachers.add(teacherForm);
> >
> > }
> >
> > The last row is stored in index 0 and 1.  I expect 0 to be the first 
> > row
> > and 1 to be the second row.
> >
> > System.out.println("teachers[0]: " + teachers.get(0));
> >
> > System.out.println("teachers[1]: " + teachers.get(1));
> >
> > Anyone know why?
> >
> >
> >
> > Thanks,
> >
> > Barry
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org
> 
> 
> 
> 


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


Re: DynaAction form and ArrayList

Posted by Mark Lowe <ma...@talk21.com>.
while(rs.next()) {
	Map teacher = new HashMap();
	teacher.put("firstName",rs.getString("firstName"));
	teacherList.add(teacher);
}

teacherForm.set("teachers", teacherList);

request.setAttribute ("teachers", teacherList.toArray());

Cheers Mark

On Monday, November 3, 2003, at 07:35 PM, Barry Volpe wrote:

>
>
> Only Last row is available in my ArrayList?
>
> Have the following:
>
> ArrayList teachers = new ArrayList();
> DynaActionForm teacherForm = (DynaActionForm)form;
>
> while ( rs.next() ) {
>
>
> System.out.println("firstname.rs: " + rs.getString("firstname"));
>
> System.out.println("lastname.rs: " + rs.getString("lastname"));
>
> System.out.println("units.rs: " + rs.getString("units"));
>
>
>
>
>
> teacherForm.set("firstname",rs.getString("firstname"));
>
> teacherForm.set("lastname",rs.getString("lastname"));
>
> teacherForm.set("units",rs.getString("units"));
>
>
>
>
> System.out.println("firstname.get: " + teacherForm.get("firstname"));
>
> System.out.println("lastname.get: " + teacherForm.get("lastname"));
>
> System.out.println("units.get: " + teacherForm.get("units"));
>
>
> teachers.add(teacherForm);
>
> }
>
> The last row is stored in index 0 and 1.  I expect 0 to be the first 
> row
> and 1 to be the second row.
>
> System.out.println("teachers[0]: " + teachers.get(0));
>
> System.out.println("teachers[1]: " + teachers.get(1));
>
> Anyone know why?
>
>
>
> Thanks,
>
> Barry
>
>
>
>
>
>
>
>
>
>


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


RE: DynaAction form and ArrayList

Posted by David Friedman <hu...@ix.netcom.com>.
Shouldn't you be adding rs.next()
and not repeatedly adding the 
same teachForm?  See:
> teachers.add(teacherForm); 

Shouldn't it be: (?)
teachers.add(rs); 

Regards,
David

-----Original Message-----
From: Barry Volpe [mailto:struts@childrencare.com]
Sent: Monday, November 03, 2003 2:36 PM
To: Struts Users Mailing List
Subject: DynaAction form and ArrayList




Only Last row is available in my ArrayList?

Have the following:

ArrayList teachers = new ArrayList();
DynaActionForm teacherForm = (DynaActionForm)form;

while ( rs.next() ) { 


System.out.println("firstname.rs: " + rs.getString("firstname"));

System.out.println("lastname.rs: " + rs.getString("lastname")); 

System.out.println("units.rs: " + rs.getString("units")); 





teacherForm.set("firstname",rs.getString("firstname"));

teacherForm.set("lastname",rs.getString("lastname")); 

teacherForm.set("units",rs.getString("units"));




System.out.println("firstname.get: " + teacherForm.get("firstname"));

System.out.println("lastname.get: " + teacherForm.get("lastname")); 

System.out.println("units.get: " + teacherForm.get("units")); 


teachers.add(teacherForm); 

}

The last row is stored in index 0 and 1.  I expect 0 to be the first row
and 1 to be the second row. 

System.out.println("teachers[0]: " + teachers.get(0));

System.out.println("teachers[1]: " + teachers.get(1)); 

Anyone know why?



Thanks,

Barry












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