commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mark Shifman <mark.shif...@yale.edu>
Subject Re: [DbUtils] Question about BeanProcessor
Date Mon, 25 Apr 2005 13:03:44 GMT
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


Mime
View raw message