You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Jack R." <ra...@hotmail.com> on 2002/12/05 16:59:50 UTC

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.



<!--

<data-sources>

<data-source>

<set-property property="autoCommit"

value="false"/>

<set-property property="description"

value="Example Data Source Configuration"/>

<set-property property="driverClass"

value="org.postgresql.Driver"/>

<set-property property="maxCount"

value="4"/>

<set-property property="minCount"

value="2"/>

<set-property property="password"

value="mypassword"/>

<set-property property="url"

value="jdbc:postgresql://localhost/mydatabase"/>

<set-property property="user"

value="myusername"/>

</data-source>

</data-sources>

-->

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Change Struts Example to use an actual database

Posted by Mark <ma...@libero.it>.
Umm i was asking similar questions last week to no avail..

The example in the documentation leaves one wondering just which servet it
is thats accessing the dataSource. AH .. I thought perhaps the servlet
object is something to do with the ActionServlet

I still have had no joy accessing the datasource outside the actionservlet,
which make me wonder why everyone bangs on about seperating Actions from
business logic.. 

Now I'm looking at the common's stuff which is being used for 1.1 , I don't
know if this is a knowledge deficit on my part or its a secretive secret
thing that's a secret... Ssshhhhhh.. But joking asside the commons stuff
looks like it might play the game..

The other option would jut be to do the normal jdbc stuff but pull the
values from the stuts config.xml...

----
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;



import java.sql.*;
import javax.sql.DataSource;


public class LoginAction extends Action {

/**
*
*
*
*
*/

    public ActionForward perform(
                                ActionMapping mapping,
                                ActionForm form,
                                HttpServletRequest request,
                                HttpServletResponse response)

        throws IOException, ServletException
        {

            LoginForm theForm = (LoginForm) form;

            String username = theForm.getUsername();
            String password = theForm.getPassword();
            String company = theForm.getCompany();
                   
            try {
                DataSource dataSource = servlet.findDataSource(null);
                Connection conn = dataSource.getConnection();
                
              String query = "SELECT "+
                                    "users.name,"+
                                    "FROM users,companies "+
                                    "WHERE users.username = '"+ username +"'
bla bla ";

          
                   
                Statement stmt = conn.createStatement ();
                ResultSet rs = stmt.executeQuery(query);
                HttpSession session = request.getSession();
                if(rs.next())
                {
                            return (mapping.findForward("sucess"));

                }
                else
                {
                            return (mapping.findForward("failure"));
                }

            }
            catch (SQLException e) {
                e.printStackTrace();
            }
                

        }
}



On 5-12-2002 16:59, "Jack R." <ra...@hotmail.com> wrote:

> 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.
> 
> 
> 
> <!--
> 
> <data-sources>
> 
> <data-source>
> 
> <set-property property="autoCommit"
> 
> value="false"/>
> 
> <set-property property="description"
> 
> value="Example Data Source Configuration"/>
> 
> <set-property property="driverClass"
> 
> value="org.postgresql.Driver"/>
> 
> <set-property property="maxCount"
> 
> value="4"/>
> 
> <set-property property="minCount"
> 
> value="2"/>
> 
> <set-property property="password"
> 
> value="mypassword"/>
> 
> <set-property property="url"
> 
> value="jdbc:postgresql://localhost/mydatabase"/>
> 
> <set-property property="user"
> 
> value="myusername"/>
> 
> </data-source>
> 
> </data-sources>
> 
> -->
> 
> --
> To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
> For additional commands, e-mail: <ma...@jakarta.apache.org>
> 
> 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Change Struts Example to use an actual database

Posted by "Craig R. McClanahan" <cr...@apache.org>.

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

> Date: Thu, 5 Dec 2002 09:59:50 -0600
> From: Jack R. <ra...@hotmail.com>
> Reply-To: Struts Users Mailing List <st...@jakarta.apache.org>
> To: Struts Users Mailing List <st...@jakarta.apache.org>
> 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:

  http://jakarta.apache.org/tomcat/tomcat-4.1-doc/jndi-datasource-examples-howto.html

* 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.

Craig



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>