Howdy,
I usually prefer the getObject(i) call, and then do something like
if(object instanceof Number) {
object = new Double(((Number) object).doubleValue());
}
Or something like that. But it's just a matter of style. What I don't
like about either approach is the Double being somewhat hard-coded. I
would look to make DbUtils use commons-beanutils' ConvertUtils and
related facilities rather than specific conversions.
Of course, this is wishful thinking as I have no spare bandwidth to work
on this with tomcat 5 release coming out...
Yoav Shapira
Millennium ChemInformatics
>-----Original Message-----
>From: Corby Page [mailto:[EMAIL PROTECTED]
>Sent: Tuesday, November 25, 2003 12:18 PM
>To: [EMAIL PROTECTED]
>Subject: [DbUtils]Improving the BeanHandler
>
>Hi, first off I'd like to say thanks for the DbUtils package. It has
been
>very helpful for cleaning up my code in places where I have to do
manual
>persistence.
>
>I have had to modify the code in order to use the BeanHandler, and I
wanted
>to offer my modifications as a patch to the codebase.
>
>Let me run through a quick use-case: I have a bean with a property
called
>quantity of type double. I tried to populate this bean with a SELECT
>statement using the BeanHandler.
>
>However, the BasicRowProcessor.createBean( ... ) method executes the
>following code:
>
>Object value = rs.getObject(i);
>
>The return type from my Sybase JDBC Driver here is BigDecimal. Since
this
>isn't a perfect match for the exposed bean property, it refuses to
populate
>the property.
>
>When I code this persistence operation by hand, I use rs.getDouble(i).
This
>way, I make the vendor do the work for me. I tell the vendor I expect a
>double return type, and he is responsible for performing the
>BigDecimal.doubleValue() conversion.
>
>So, I have created my own custom RowProcessor that replaces the
getObject
>call from above with a method that looks like this:
>
> Object value;
> if ( propType.equals( Double.TYPE ) || propType.equals(
>Double.class ) )
> {
> value = new Double( rs.getDouble( index ) );
> }
>....
> else
> {
> value = rs.getObject( index );
> }
>
>This is implemented for all of the appropriate ResultSet methods of
course.
>Now, we look at the type of the named property, and we call the
appropriate
>method of the JDBC driver to get the vendor to adapt to that type, if
>possible. This feels a lot more flexible than requiring a bean property
to
>precisely match a (possibly arbitrary) default type used by the JDBC
>vendor.
>
>I would be happy to offer this code as a patch to BasicRowProcessor, as
an
>alternate RowProcessor that extends BasicRowProcessor, or leave it up
to
>you guys to implement if you feel this is a good idea.
>
>Thanks,
>Corby
>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [EMAIL PROTECTED]
>For additional commands, e-mail: [EMAIL PROTECTED]
This e-mail, including any attachments, is a confidential business communication, and
may contain information that is confidential, proprietary and/or privileged. This
e-mail is intended only for the individual(s) to whom it is addressed, and may not be
saved, copied, printed, disclosed or used by anyone else. If you are not the(an)
intended recipient, please immediately delete this e-mail from your computer system
and notify the sender. Thank you.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]