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." <[EMAIL PROTECTED]> 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:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>
> 
> 


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

Reply via email to