--- Andy Cheng <[EMAIL PROTECTED]> wrote:
> Hi, currently I have something like the code below on every action, is
> this the normal way of getting the connection?  And how would normal
> people do if they want to make some data access objects, which contain
> all the methods like insert, select etc, while the Action class will
> purely do logic, for example only contain this kind of statements:
> HashMap hmResult = dataAccess.getAllClients();  Thanks.

Most apps lookup the DataSource from JNDI.  You configure the DataSource
in your container instead of struts-config.xml.

> 
>     public ActionForward perform(ActionMapping mapping, ActionForm form,
>     HttpServletRequest request, HttpServletResponse response) {
>         ServletContext context = servlet.getServletContext();
>         try {
>             DataSource dataSource =
> (DataSource)context.getAttribute(Action.DATA_SOURCE_KEY);
>             Connection conn = dataSource.getConnection();
>             Statement stmt = conn.createStatement();
>             ResultSet rs = stmt.executeQuery("SELECT * FROM aTable");
>             while(rs.next()) {
>                 //...
>             }
>             
>         } catch (Exception e) {
>             e.printStackTrace();
>         }
>         return (mapping.findForward("xxx"));
>       }

This is absolutely the wrong way of doing database access.  You *must*
properly cleanup JDBC resources on every access, including when exceptions
are thrown.  Doing this cleanup is tedious, boring, and error prone so I
created the Mapper project in the Jakata Commons Sandbox.  Check it out;
it significantly reduces the size of database access code as well as
isolates your app. from the persistence mechanism so you can swap it
later.

David

> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to