• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Iterating through a collection

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am fetching data form a database using this code:
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(sURL, sUsername, sPassword);
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT * FROM users");
RowSetDynaClass rsdc = new RowSetDynaClass(res);
rows = rsdc.getRows();
request.setAttribute("coll", rows);

In my jsp page I want to iterate through that collection and prints out every item in it, I've tried this:
<logic :present name="coll">
<logic:iterate id="contents" name="coll" >
<bean:write name="contents" /><br>
</logic:iterate>
</logic :present>

ant the stack trace says: JspException: No getter method for property value of bean contents

The problem is that I don't know how to do this, I know how to iterate through e.g. a hashmap using something like this:
Map content: <bean:write name="contents" property="key"/> <bean:write name="contents" property="value"/><br>

How do I do something similar with a collection? Please someone shed some light on this, thanks.

[ August 26, 2007: Message edited by: Mattias Sarling ]

[ August 26, 2007: Message edited by: Mattias Sarling ]

[ August 26, 2007: Message edited by: Mattias Sarling ]
[ August 26, 2007: Message edited by: Mattias Sarling ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The statement:

will not work because you can't just display a dynabean. You must display some property of the bean corresponding to a column name in the database. Example:
 
Matt Sall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I have tried using this:
<bean:write name="contents" property="user_id" />
but I get the same error message. What if wanted to print out every name/value pair in the collection and I didn't know the columns in the DB, I'm using this "SELECT * FROM users". How can I do that, e.g. in plain java code if there's no way of doing it with tags?

Thanks.
 
Matt Sall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problem solved, thanks anyway!
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mattias,

I'm glad you solved your problem. That's good news.

Since these forums are searchable, it would be helpful if you would take a few minutes to describe what you did to solve the problem. This will be helpful to anyone who runs in to the same problem and searches the forum for a solution.
 
Matt Sall
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I used this code to iterate through the collection and print them:

Iterator it = col.iterator();
DynaBean dyna = (DynaBean) it.next();
DynaProperty[] props = dyna.getDynaClass().getDynaProperties();

for (int i = 0; i < props.length; i++) {
String propName = props[i].getName();
String propType = props[i].getType().getName();
String propVal = (String) dyna.get(propName);
System.out.println("Dyna props: " + i + " : " + propName + " : " + propVal + "\n");



}
 
reply
    Bookmark Topic Watch Topic
  • New Topic