Matt, If you pull the scaffold project from http://sourceforge.net/project/showfiles.php?group_id=49385, there is a class called ResultSetUtils which provides the below methods for mapping a ResultSet to a Collection of a given class or returning a given element from the ResultSet. It uses reflection and the meta data of the resultSet in conjunction with BeanUtils and PropertyUtils to invoke the setters on the POJO.
Seems to work fairly well. Regards, Todd G. Nist /** * Populate target bean with the first record from a ResultSet. * * @param resultSet The ResultSet whose parameters are to be used * to populate bean properties * @param target An instance of the bean to populate * @exception SQLException if an exception is thrown while setting * property values, populating the bean, or accessing the ResultSet * @returns True if resultSet contained a next element */ public static boolean getElement(Object target, ResultSet resultSet) throws SQLException { .... } /** * Return a ArrayList of beans populated from a ResultSet. * * @param resultSet The ResultSet whose parameters are to be used * to populate bean properties * @param target An instance of the bean to populate * @exception SQLException if an exception is thrown while setting * property values, populating the bean, or accessing the ResultSet */ public static Collection getCollection(Object target, ResultSet resultSet) throws SQLException { .... } -----Original Message----- From: news [mailto:[EMAIL PROTECTED] Behalf Of Vic Cekvenich Sent: Saturday, September 06, 2003 3:59 AM To: [EMAIL PROTECTED] Subject: Re: Converting a ResultSet to a List of POJOs There are no people more expert in Struts than you Matt. I think you can use RowSet to wrap a ResultSet, and RowSet has a toCollection or getCollection method. (Or just don't user ResultSet at all, just use RowSet, Sun has "default" implementation in JDC, but Orcale has an extra download, other do to). Worst case you write your own helper object. But... I would use a DAO layer(and not code to JDBC), it will be cheaper/faster in the long run. Consider writing a console generator (I have one, in CVS at bP for iBatis that jsut enmerates all tables in DB) that takes a SQL Strings[] in XML file, and based on that queries the DB, and writes a real DAO and beans (and can do this anytime, for any of your projects). (I do not know how well Hibernate works with SQL). .V Matt Raible wrote: > Dear Struts Experts, > > I recently started a new project where most of the backend code is already > written with JDBC and ResultSets. The ResultSets are iterated through and a > POJOs values are set using pojo.setName(rs.getString("...")), etc. - you get > the point. I'm wondering if there's an easier way - so I could do something > like this: > > ResultSet rs = stmt.executeQuery("SELECT ..."); > List objects = FancyUtilitity.convertResultSetToListOfObjects(rs, > object.class); > > Hibernate let me do this very simply - and I miss the fact that I could type > a line or two to get a List of POJOs. > > List users = ses.createQuery("from u in class " + User.class > + "order by u.name").list(); > > I've looked at the RowSetDynaClass (http://tinyurl.com/mekh), which has an > interesting way of doing this - is this the "recommended" approach in the > JDBC world? Here's an example using it: > > 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 > > > Thanks, > > M --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]