You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Brandon Mercer <br...@eutonian.com> on 2005/01/30 17:02:15 UTC

Accessing Database Information

Hello,
I've got Ted Husted's book Struts in Action and I really like it.  I've 
also got another book Professional Jakarta Struts.  They both talk about 
accessing information from a database and DAO and DTO and all that jazz 
and quite frankly I don't understand it.  :-P  Right now I'm in the 
middle of this rather sophisticated application and I'm starting to 
wonder if I could be doing things a lot more easily.  For instance, when 
I retrieve information from a Database I do something like this in my 
action:

ArrayList results = BillpayData.getAssets(
                         account, getDataSource(request, "transactions"));

And I have the BillpayData class to handle getting the information from 
the DB and set it into a bean like:

public class BillpayData {

    public static ArrayList getAssets(
    final String account,
        final DataSource dataSource)
        throws Exception {

        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        BillpayForm atnlist = null;
    ArrayList result = new ArrayList();

        try {
            conn = dataSource.getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery(
        "select * from assets where account=\"" + account + "\"");
       
            // While there are results from our query
        // create the codes bean with the proper
        // values from the database.
            while (rs.next()) {
        atnlist = new BillpayForm();
       
                atnlist.setAtn(rs.getString("atn"));
        atnlist.setDescription(rs.getString("description"));
       
        result.add(atnlist);
        }
       
    } finally {
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }

        }
    return result;
    }
}
  BUT... I see all these handy examples about setting that stuff in a 
bean using beanutils and it makes me think I'm doign way too much work!  
Does anyone have an explination of what Data Access Object structure I 
should be using (or is the way I'm doing things ok)?  And does anyone 
have any simple applications that show a good way to use those beanutils 
(as I'm not very good with java just yet).  Thanks for your help.
Brandon

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Accessing Database Information

Posted by Larry Meadors <la...@gmail.com>.
Read Rick's ibatis tutorial, it covers DAO quite well:

http://reumann.net/struts/ibatisLesson1.do


On Sun, 30 Jan 2005 09:57:03 -0700, Wendy Smoak <ja...@wendysmoak.com> wrote:
> From: "Brandon Mercer" <br...@eutonian.com>
> > They both talk about accessing information from a database and DAO and DTO
> > and all that jazz and quite frankly I don't understand it.  :-P
> 
> This should explain the DAO and DTO references:
> http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html
> 
> This is the pattern I used to write my data access layer.  But I'm unlucky
> in that my database is weird and SQL/JDBC doesn't work well with it.  It has
> a proprietary API, so I had to write all the DAO classes myself.  It looks
> like you have a normal SQL database, so you could use one of the persistence
> frameworks like Hibernate if you wanted.
> 
> What you've got so far looks pretty good!  From your intro I expected to see
> JDBC in the Action code, but you have it separated out. :)
> 
> Is 'BillPayForm'  Struts form bean?  If so, [IMO] you should get it out of
> your BillpayData class.  (The data access classes shouldn't know about
> Struts or HTTPRequests.)  You might also consider having the data access
> class "know" how to get its own connection rather than passing one in.  I
> use a static Factory, but you can [I hear] configure a Datasource in the
> Servlet container and then any class should be able to look it up [JNDI??].
> 
> The magic of BeanUtils looks like this:
> 
>    Person person = personDAO.read( "012345" );
>     BeanUtils.copyProperties( form, person );
> 
> That prepopulates the form for display.
> When the user has submitted his changes, you go back the other way:
> 
>    BeanUtils.copyProperties( person, form );
>     personDAO.update( person );
> 
> --
> Wendy Smoak 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Accessing Database Information

Posted by Wendy Smoak <ja...@wendysmoak.com>.
From: "Brandon Mercer" <br...@eutonian.com>
> They both talk about accessing information from a database and DAO and DTO 
> and all that jazz and quite frankly I don't understand it.  :-P

This should explain the DAO and DTO references:
http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html

This is the pattern I used to write my data access layer.  But I'm unlucky 
in that my database is weird and SQL/JDBC doesn't work well with it.  It has 
a proprietary API, so I had to write all the DAO classes myself.  It looks 
like you have a normal SQL database, so you could use one of the persistence 
frameworks like Hibernate if you wanted.

What you've got so far looks pretty good!  From your intro I expected to see 
JDBC in the Action code, but you have it separated out. :)

Is 'BillPayForm'  Struts form bean?  If so, [IMO] you should get it out of 
your BillpayData class.  (The data access classes shouldn't know about 
Struts or HTTPRequests.)  You might also consider having the data access 
class "know" how to get its own connection rather than passing one in.  I 
use a static Factory, but you can [I hear] configure a Datasource in the 
Servlet container and then any class should be able to look it up [JNDI??].

The magic of BeanUtils looks like this:

   Person person = personDAO.read( "012345" );
    BeanUtils.copyProperties( form, person );

That prepopulates the form for display.
When the user has submitted his changes, you go back the other way:

   BeanUtils.copyProperties( person, form );
    personDAO.update( person );

-- 
Wendy Smoak 



---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org