Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 10986 invoked from network); 25 Apr 2005 13:05:31 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur.apache.org with SMTP; 25 Apr 2005 13:05:31 -0000 Received: (qmail 47157 invoked by uid 500); 25 Apr 2005 13:05:48 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 47136 invoked by uid 500); 25 Apr 2005 13:05:48 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 47097 invoked by uid 99); 25 Apr 2005 13:05:47 -0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests= X-Spam-Check-By: apache.org Received-SPF: pass (hermes.apache.org: local policy) Received: from biomed.med.yale.edu (HELO BIOMED.MED.YALE.EDU) (130.132.232.48) by apache.org (qpsmtpd/0.28) with ESMTP; Mon, 25 Apr 2005 06:05:47 -0700 Received: from [128.36.123.85] (moussaka.med.yale.edu [128.36.123.85]) by biomed.med.yale.edu (PMDF V6.1-1 #30532) with ESMTPS id <01LNI7JEYH2I012RFV@biomed.med.yale.edu> for commons-user@jakarta.apache.org; Mon, 25 Apr 2005 09:04:25 -0400 (EDT) Date: Mon, 25 Apr 2005 09:03:44 -0400 From: Mark Shifman Subject: Re: [DbUtils] Question about BeanProcessor In-reply-to: <426CC0F8.7030203@gmx.de> To: Jakarta Commons Users List Message-id: <426CEAB0.9020205@yale.edu> MIME-version: 1.0 Content-type: text/plain; format=flowed; charset=ISO-8859-1 Content-transfer-encoding: 7bit X-Accept-Language: en-us, en User-Agent: Mozilla Thunderbird 1.0 (X11/20041206) References: <426CC0F8.7030203@gmx.de> X-Virus-Checked: Checked X-Spam-Rating: minotaur.apache.org 1.6.2 0/1000/N Hi: From http://jakarta.apache.org/commons/dbutils/examples.html QueryRunner run = new QueryRunner(dataSource); // Use the BeanHandler implementation to convert the first // ResultSet row into a Person JavaBean. ResultSetHandler h = new BeanHandler(Person.class); // Execute the SQL statement with one replacement parameter and // return the results in a new Person object generated by the BeanHandler. Person p = (Person) run.query( "SELECT * FROM Person WHERE name=?", "John Doe", h); This works just fine and you don't really need to do anything fancy. The source code is here http://svn.apache.org/viewcvs.cgi/jakarta/commons/proper/dbutils/trunk/src/java/org/apache/commons/dbutils/ and shows how they do everything. I am using LazyDynaBeans (org.apache.commons.beanutils.LazyDynaBean) so I wrote my own processor and handler, see below: import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.beanutils.LazyDynaBean; public class LazyDynaBeanProcessor { public Object toBean(ResultSet rs) throws SQLException { LazyDynaBean ldb = new LazyDynaBean(); ResultSetMetaData rsmd = rs.getMetaData(); for (int i = 1; i <= rsmd.getColumnCount(); i++) { String name = rsmd.getColumnName(i).toLowerCase(); Object thecol =rs.getObject(i); if(thecol == null) //my weirdness if null make the null string ldb.set(name, ""); else ldb.set(name, rs.getObject(i)); } return ldb; } public Object toBeanList(ResultSet rs) throws SQLException { List results = new ArrayList(); while (rs.next()) { results.add(this.toBean(rs)); } return results; } } --------------- import java.sql.ResultSet; import java.sql.SQLException; import org.apache.commons.dbutils.ResultSetHandler; public class LazyDynaBeanHandler implements ResultSetHandler{ private LazyDynaBeanProcessor convert = new LazyDynaBeanProcessor(); public Object handle(ResultSet rs) throws SQLException{ return rs.next() ? convert.toBean(rs) : null; } } --------- import java.sql.ResultSet; import java.sql.SQLException; import org.apache.commons.dbutils.ResultSetHandler; public class LazyDynaBeanListHandler implements ResultSetHandler{ private LazyDynaBeanProcessor convert = new LazyDynaBeanProcessor(); public Object handle(ResultSet rs) throws SQLException{ return convert.toBeanList(rs); } } Sascha Meyer wrote: > Hi there, > > I'd like to use the BeanProcessor found in org.apache.commons.dbutils, > but I haven't found any examples of how to populate a bean through the > BeanProcessor ... especially the second parameter (java.lang.Class > type) of the method "toBean" confuses me, because I couldn't find any > info what exactly should be passed to the method. > > Could you give me a short usage example? > > Thank you! > > Sascha > > > --- > avast! Antivirus: Ausgehende Nachricht sauber. > Virus-Datenbank (VPS): 0516-7, 22.04.2005 > Getestet um: 25.04.2005 12:05:45 > avast! - copyright (c) 2000-2004 ALWIL Software. > http://www.avast.com > > > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org > For additional commands, e-mail: commons-user-help@jakarta.apache.org > -- Mark Shifman MD. Ph.D. Yale Center for Medical Informatics Phone (203)737-5219 mark.shifman@yale.edu --------------------------------------------------------------------- To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org For additional commands, e-mail: commons-user-help@jakarta.apache.org