In general, you would might want to write a helper class to handle the
database access. Struts doesn't need to know where the data comes from
or goes to. This isn't easy to do with the GenericDataSource in the
Struts package, since it is tied to the Servletcontext, so I use PoolMan
(www.codestudio.com) instead. 

Basically, I might have a method like 

Data.articleUpdate(thisForm.get("article"),
            thisForm.get("contributor"),
            thisForm.get("creator"),
            thisForm.get("title"),
            thisForm.get("content")
        );

Where Data is a helper with no links to the HTTP layer that handles all
the data access. This is the most the Action sees regarding data access.

For larger datasets, I might have a method on Data that takes a Map, and
retrieves the properties that way. Most of my objects can create Maps of
themselves these days, since that plays well with BeanUtils.populate().
Another way to go might be to create a bean that represented the
ResultSet (or equivalent), which you could populate and pass to a helper
calss.

To attach to the database, I use another helper method that returns the
connection to Data (so that class doesn't actually know where the
resource layer is either). 

public final class ConnectionPool {

    /**
     * An exception message to throw if datasource is null.
     */
    public static final String DATASOURCE_ERROR = "Connection pool " +
        "not available. Check your poolman.xml config, and be sure " +
        " you are using a valid dbname parameter (use dbname, not
jndiName)";

    /**
     * The application scope attribute under which our JNDI datasource
     * context is stored.
     */
    public static final String JNDI_CONTEXT_KEY = "java:comp/env";

    /**
     * The application scope attribute under which our JNDI datasource
     * is stored.
     */
    // public static final String JNDI_DATASOURCE_KEY = "jdbc/artimus";
    public static final String JNDI_DATASOURCE_KEY = "artimus";

    /**
     * The application scope JNDI path under which our datasource
     * is stored.
     */
    public static final String JNDI_NAME_KEY =
"java:comp/env/jdbc/artimus";

    /**
     * Returns a JDBC connection from a connection pool or other
     * resource, to be used and closed promptly.
     * <p>
     * @returns JDBC connection from resource layer.
     * @exception SQLException on SQL or other errors. May wrap other
     * exceptions depending on implementation. Will not return null.
     */
    public static final Connection getConnection() throws SQLException {
        DataSource ds = PoolMan.findDataSource(JNDI_DATASOURCE_KEY);
        if (ds==null)
            throw new SQLException(DATASOURCE_ERROR);
        return(ds.getConnection());
    }
}

On the return trip, I use a BeanUtils.populate() derivative to move the
ResultSet into an arbitrary bean or collection of beans. These could be
ActionForm beans or any other. The method doesn't care. See 

http://www.mail-archive.com/struts-user@jakarta.apache.org/msg14397.html

for more on that.

If you are new tp J2EE application design, be sure to absorb the
BluePrints, 

http://java.sun.com/j2ee/blueprints/

and consider investing in tthe Core J2EE Patterns book.

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel +1 716 737-3463
-- http://www.husted.com/about/struts/


[EMAIL PROTECTED] wrote:
> 
> Hi all,
> 
> I'm new to J2EE application design. I would like to get advices on how to
> best get access to DB in struts&J2EE applications (through datasource).
> I don't want to code JDBC access in all struts action classes that need to
> access database. What would be the best way to access database ?
> Would it be possible from action classes to give control to a DataBase
> Controller that would be in charge of accessing database ? How does this
> controller may look like ?
> Any suggestion, advices, experience feedback would be greatly appreciated.
> 
> Regards
> 
> Bruno

Reply via email to