commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Henri Yandell <bay...@generationjava.com>
Subject Re: [DbUtils] Using BeanUtils converters
Date Mon, 29 Dec 2003 15:38:55 GMT

I've pulled the converters out to the Commons Convert library in the
sandbox by the way, so any dependency you might want to add doesn't have
to link to all of beanutils.

I'm using it in a couple of my own libraries to see how it feels etc.

Hen

On Mon, 29 Dec 2003, David Graham wrote:

> Rather than add the dependency we added the ColumnProcessor interface.
> See the process() method javadocs here:
>
> http://jakarta.apache.org/commons/dbutils/apidocs/org/apache/commons/dbutils/ColumnProcessor.html#process(java.sql.ResultSet,%20int,%20java.lang.Class)
>
> You can implement this interface and use BeanUtils without adding a
> dependency to DbUtils and without changing BasicRowProcessor.
> ColumnProcessor will be included with 1.1 so let us know how it works for
> you or if it could be improved before release.
>
> David
>
>
> --- Jorge Morales Pou <jmp07@tid.es> wrote:
> > Hi,
> >   I´m using the DbUtils component in a web application, and I´m
> > obtaining
> >   datatypes from the database which are not converted to the proper DTO
> > data
> >   type. For instance, I´m getting a Short from a table in a database
> > which I
> >   have mapped to an Integer in my DTO. This conversion is not happening,
> > since
> >   isCompatibleType in BasicRowProcessor is returning false.
> >   I have changed the implementation of the callSetter method, in the
> > same class,
> >   so it uses ConvertUtils from the BeanUtils package.
> >   It works great with basic data types conversion, and if you want an
> > explicit
> >   data type conversion, you just have to create a custom Converter,
> > which I´ve
> >   been doing all over my app.
> >   I know this introduces a dependency on the BeanUtils package, but
> > since I use
> >   it for other parts of my app, it´s no problem.
> >
> >     private void callSetter(
> >         Object target,
> >         PropertyDescriptor prop,
> >         Object value)
> >         throws SQLException {
> >
> >         Method setter = prop.getWriteMethod();
> >
> >         if (setter == null) {
> >             return;
> >         }
> >
> >         Class[] params = setter.getParameterTypes();
> >         try {
> >             // Don't call setter if the value object isn't the right
> > type
> >             if (this.isCompatibleType(value, params[0])) {
> >                 setter.invoke(target, new Object[] { value });
> >             }else{
> >                 Converter converter = ConvertUtils.lookup(params[0]);
> >                 // If there is a converter for the required data type,
> > invoke
> >                 // the setter method
> >                 if (converter != null)
> >                     setter.invoke(target, new Object[] {
> > converter.convert(params[0], value) });
> >             }
> >
> >         } catch (IllegalArgumentException e) {
> >             throw new SQLException(
> >                 "Cannot set " + prop.getName() + ": " + e.getMessage());
> >
> >         } catch (IllegalAccessException e) {
> >             throw new SQLException(
> >                 "Cannot set " + prop.getName() + ": " + e.getMessage());
> >
> >         } catch (InvocationTargetException e) {
> >             throw new SQLException(
> >                 "Cannot set " + prop.getName() + ": " + e.getMessage());
> >         }
> >     }
> >
> >
> >     I hope this helps anybody, and probably can be an enhacement to
> > version 1.1
> >
> > Happy holidays and happy new year from Spain,
> > -----------------------
> > "He pasado una noche estupenda.
> >  Pero no ha sido esta."
> >  GROUCHO MARX
> > -----------------------
> > Jorge Morales Pou
> > jmp07_ARROBA_tid.es
> > Telefónica I+D, Madrid.
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: commons-user-help@jakarta.apache.org
> >
>
>
> __________________________________
> Do you Yahoo!?
> Free Pop-Up Blocker - Get it now
> http://companion.yahoo.com/
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
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