I implemented something a little more memory-efficient than this (doesn't require the entire result set to be in memory) in tonight's nightly build of commons-beanutils, which will therefore be available in the 20020713 nightly build of Struts. You use it something like this:
Connection conn = ...; Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from customers"); Iterator rows = (new ResultSetDynaClass(rs)).iterator(); while (rows.hasNext()) { DynaBean row = (DynaBean) rows.next(); System.out.println("Processing customer " + row.get("account_id")); ... access this row as a DynaBean ... } rs.close(); stmt.close(); I elected to avoid doing the type conversions, so the properties you get back will correspond to their types in the database. Craig On Fri, 12 Jul 2002 [EMAIL PROTECTED] wrote: > Date: Fri, 12 Jul 2002 13:56:38 -0400 > From: [EMAIL PROTECTED] > Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]> > To: [EMAIL PROTECTED] > Subject: RE: DynaBeans, DynaClass, DynaMen > > > > Here is what I am using... Very simple and only returns strings... > > > /** > * Converts a resultset into an ArrayList of DynaBeans > * > * @param resultSet SQL result set to be converted > * @return ArrayList of DynaBeans with all columnnames converted to > * lowercase > * @throws SQLException DOCUMENT ME! > */ > private static ArrayList getDynaBeanArrayList(ResultSet resultSet) > throws SQLException { > > ResultSetMetaData metaData = resultSet.getMetaData(); > int cols = metaData.getColumnCount(); > ArrayList list = new ArrayList(); > DynaProperty[] props = new DynaProperty[cols]; > BasicDynaClass dClass = null; > > for (int i = 1; i <= cols; i++) { > props[i - 1] = new > DynaProperty(metaData.getColumnName(i).toLowerCase()); > } > > try { > dClass = new BasicDynaClass("test", > Class.forName( > > "org.apache.commons.beanutils.BasicDynaBean"), > props); > } catch (Exception e) { > e.printStackTrace(); > } > > while (resultSet.next()) { > > HashMap map = new HashMap(cols, 1); > > for (int i = 1; i <= cols; i++) { > map.put(metaData.getColumnName(i).toLowerCase(), > resultSet.getString(i)); > } > > try { > > DynaBean dbean = dClass.newInstance(); > BeanUtils.populate(dbean, map); > list.add(dbean); > } catch (Exception e) { > e.printStackTrace(); > throw new SQLException("RequestUtils.getArrayList: " > + e.toString()); > } > } // End While > > return (list); > } > > > -----Original Message----- > From: craigmcc [mailto:[EMAIL PROTECTED]] > Sent: Friday, July 12, 2002 12:07 PM > To: struts-user > Subject: Re: DynaBeans, DynaClass, DynaMen > > > > > On Fri, 12 Jul 2002, Thorbjoern Ravn Andersen wrote: > > > Date: Fri, 12 Jul 2002 07:02:57 +0200 > > From: Thorbjoern Ravn Andersen <[EMAIL PROTECTED]> > > Reply-To: Struts Users Mailing List <[EMAIL PROTECTED]> > > To: Struts Users Mailing List <[EMAIL PROTECTED]> > > Subject: Re: DynaBeans, DynaClass, DynaMen > > > > [EMAIL PROTECTED] skrev: > > > > >...anyone remember DynaMen? > > > > > >Anyhow... I got a Dynabean mechanism working that builds a DynaBean > > >based on the metadata of a SQL result set, populates and array of the > > >little buggers and passes it back to me. For displaying I have a tag > > >library that does not like a call to get('name') as the field name. > > >What is the best way to get around this? > > > > > I wrote an AnonyBeans package which uses BCEL to generate beans on the > > fly based on a ResultSet. It is alpha code but works for me, and is > > usable anywhere where you need a real traditional bean, but where you > do > > not want to serialize it or use its type in Java source. > > > > Is this interesting? > > > > I think it would be interestesting, even though it might not be > universally useful (some containers won't let you introduce new classes > at > runtime). > > I'd also be interested in a mechanism that converted a ResultSet into a > custom DynaClass, with a corresponding DynaBean for each row. This > would > be trivially simple to do -- so simple that it probably makes a > worthwhile > addition to commons-beanutils itself if someone wanted to take this on. > > This wouldn't help you create dynamic input forms, but it would make a > completely flexible bean-like wrapper around a result set so you can use > Struts tags to display stuff. > > > -- > > Thorbjørn Ravn Andersen http://biobase.dk/~tra > > Craig > > > -- > To unsubscribe, e-mail: > <mailto:[EMAIL PROTECTED]> > For additional commands, e-mail: > <mailto:[EMAIL PROTECTED]> > > > > -- > To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> > For additional commands, e-mail: <mailto:[EMAIL PROTECTED]> > > -- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>