This is what I have been thinking about a long time.
Its often we need a simple and lightweight db
interface for adhoc needs.
I have not created it but have been planning on doing
it. I was not aware Velosurf has DB helper classes, I
may give it a try.
My idea is to give wrappers/helper classes that work
fine within servlet environment or standalone.
* servlet env: use named jdbc resource declared in
context.xml description to transparently use container
pooling.
* standalone: use DriverManager or DataSource jdbc
clsses directly
* wrapper returns iterator, and iter.next() steps
forward the resultset object.
* It should NOT read all rows to an abstract container
first and then gives iterator over it. This is an
overkill and/or consumes unnecessary resources.
* example of the .vm template script.
#foreach( $row in $DBUtil.execute("resMyDB", "Select *
>From customer") )
custId=$row.get("id")
custName=$row.get("name")
isActive=$row.getBoolean("active")
group=$row.getInt("group", -1)
#end
But I have one question and don't know how to
implement it in Velocity.
1) try-finally to always call sqlObj.close() methods
If there is an exception within .vm, how do I release
sql objects (rs.close, stmt.close, conn.close).
Amd same calls should be done after successfull
template call.
.vm file should not have any releaseObject calls, but
automatically at the end of mergeTemplate process.
Sort of lifetime interface.
ContextVariableLifetime interface implemented by
DBUtil iterator object.
* beforeMerge: called before merge to start
* onSuccess: merge successfull
* onError: merge exception encountered
--- Claude Brisson <[EMAIL PROTECTED]> wrote:
> Hi,
>
> the best practice is to use an object relational
> mapping library to
> avoid disseminating sql statements inside your
> templates. Also, updates
> to the database should be performed by a controller
> and not from inside
> the templates.
>
> I agree with you that Hibernate is overkill in many
> situations, and
> that's why I initially started the Velosurf project.
> Maybe it can
> fulfill your needs.
>
> http://velosurf.sourceforge.net
>
> the 2.2 release should be published quite soon, so I
> invite you to use
> the svn head rather than the 2.1 release if you want
> to go further than
> testing it. Feedback is welcome.
>
> There are also some other persistence and o/r
> mapping libraries besides
> Hibernate you may want to check: iBatis, jGrinder,
> dmLite, Melati,
> Cayenne, TinyMarbles, Odal, Carbonado, SimpleORM...
>
>
> Claude
>
>
> Le vendredi 13 juillet 2007 à 17:38 +1000, Joe
> Kramer a écrit :
> > Hello,
> >
> > I need to compile templates based on database
> ResultSet rows.
> >
> > I have application properties, something like
> > database.statement=SELECT name,surname FROM user
> > template.name=sometemplate.vm
> >
> > sometemplate.vm:
> > <body>Hello $db.firstname $db.surname !</body>
> >
> > Java meta-code:
> > conn = DriverManager.getConnection(...)
> > result = statement.executeQuery(<statement from
> properties above>);
> > while (result.next()) {
> >
>
context=SomeMagicClassThatWillMergeResultToContext.mergeRow(result);
> > template= template =
> Velocity.getTemplate(<template from properties>);
> > template.merge(context, writer);
> > }
> >
> > My question: is there any simple solutions to map
> DB ResultSet into
> > context or HashMap (without involving complex J2EE
> > stuff/Spring/Hibernate)?
> > What is the best practice anyway?
> >
> > Thanks.
____________________________________________________________________________________
Bored stiff? Loosen up...
Download and play hundreds of games for free on Yahoo! Games.
http://games.yahoo.com/games/front
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]