DBUtils code look like this:
QueryRunner run = new QueryRunner(dataSource);
ResultSetHandler h = new BeanHandler(Person.class);
Person p = (Person) run.query("SELECT * FROM Person WHERE name=?",
"John Doe", h);
(Which implictly calls the public no arguments constructor of People
and some setters thereafter.)
I avoided that approach because it hardwires the attribute names of
the relation returned by the query with the properties of the Java
class.
So if something is called given_name in a database table there would
have to be a setGivenName (or maybe even setGiven_name) in the Java
class.
Also you would have to have public setters for all properties -
eventhough it only makes sense setting them at construction (i.e.
"id").
On Feb 24, 4:27 pm, Mark Fortner <[email protected]> wrote:
> Hi Christian,
> You might want to have a look at Apache Commons DBUtils which uses similar
> syntax to convert resultsets into beans using a the BeanProcessor class.
>
> Hope this helps,
>
> Mark
>
> On Sun, Feb 22, 2009 at 6:21 AM, Christian Hvid
> <[email protected]>wrote:
>
>
>
>
>
> > Hi Java people.
>
> > I have been toying with simplier ways of doing embedded SQL in Java.
>
> > And would like your comments on this one?
>
> >http://code.google.com/p/chalkmine/
>
> > It allows you to write code like this:
>
> > openConnection();
> > try {
> > int count = queryScalar(Integer.class, "select count(*) from
> > people");
> > System.out.println("There are "+count+" people in the bin.");
> > } finally {
> > closeConnection();
> > }
>
> > or
>
> > openConnection();
> > try {
> > List<Person> people = queryList(Person.class, "select name,
> > time_in_the_bin from people");
> > for (Person p : people)
> > System.out.println(p.getName()+" has been "+p.getTimeInTheBin()
> > +" hours in the bin.");
> > } finally {
> > closeConnection();
> > }
>
> > (Provided that Person has a constructor matching the types of name,
> > time_in_the_bin. Probably Person(String, int).)
>
> > Where the methods openConnection, queryScalar, queryList,
> > closeConnection are statically imported.
>
> > openConnection() figures out the name of the calling class, looks up a
> > configuration, opens a connection and puts in a ThreadLocal container.
>
> > queryScalar(Class, String, ...) performs a query with a single row
> > result that is "cast" to the given class.
>
> > queryList(Class, String, ...) performs a query and returns the result
> > as a list of the given class.
>
> > I would like to turn it into a full-fledged open source project.
>
> > But since it is incredibly hard for a new open source project to gain
> > traction I would like to figure out whether it is interesting enough
> > first.
>
> > -- Christian
>
> --
> Mark Fortner
>
> blog:http://feeds.feedburner.com/jroller/ideafactory
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---