struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: Change Struts Example to use an actual database
Date Fri, 06 Dec 2002 02:54:31 GMT

On Thu, 5 Dec 2002, Jack R. wrote:

> Date: Thu, 5 Dec 2002 09:59:50 -0600
> From: Jack R. <>
> Reply-To: Struts Users Mailing List <>
> To: Struts Users Mailing List <>
> Subject: Change Struts Example to use an actual database
> Hi,
> I am learning how Struts works with an database, so I am trying to change
> Struts Example (struts-example.war) to use an actual database for user data.
> Can you please tell me what I need to do to achieve that? I notice it has
> the following data source configuration in struts-config.xml, do I just need
> to change that to point to my database? How should I create a table in my
> database first? And where in the code is the user data being writtin to the
> database? I can only find code for MemoryDatabase.
> Thanks for any help.

The example app itself doesn't have the code for JDBC-based access to this
kind of data, but it would be fairly straightforward to add (if you start
from the 1.1 version of the example app sources):

* In the examples source code, you'll see an interface called
  UserDatabase.  This is an example of the Data Access Object
  (DAO) design pattern, because it deals with the logical business
  data of the app, and hides the details of how it is actually
  accessed.  All the business logic in the example app interacts
  through this interface, so you can switch the implementation
  underneath the covers to store the data anywhere you want.

* You'll need to create an implementation of this interface, as
  well as the User and Subscription interfaces, which reads and
  writes to a database (the existing MemoryUserDatabase implementation
  reads and writes to an XML file).

* When designing the UserDatabase implementation, you'll need to
  figure out how to gain access to the JDBC connection that you
  need.  The simplest method is probably to use the default data
  source for the current webapp, which you can make accessible
  to your UserDatabase implementation (among other ways) by passing
  it to the constructor as a java.sql.DataSource object.

* Alternatively, if you're running on a platform that supports
  JNDI resources (like Tomcat 4.x or later), you might want to
  use that approach instead.  The advantage is that your code
  can get to the data source without having any explicit passing
  of a DataSource or Connection variable.  For more info, see:

* In the struts-config.xml file for the example app, you'll see
  UserDatabase implementation that should be used.  Modify the
  class name to be a new one for your implementation, and customize
  the <set-property> methods to set whatever configuration properties
  your UserDatabase implementation needs (may not need any for this
  particular scenario).

* Of course, you'll need to set up database username and password,
  and the table structures your DAO implementation expects, in the
  database that is pointed at by the configuration properties.


To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message