wood burning stoves*
The moose likes Struts and the fly likes How to store the data entered by user in a form into a database using struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to store the data entered by user in a form into a database using struts" Watch "How to store the data entered by user in a form into a database using struts" New topic
Author

How to store the data entered by user in a form into a database using struts

Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98
Hi,

I developed an application that contains multiple forms. After entering the data, Again am just displaying the data which was entered by user as output, but am not storing in any database. If i want to store the data into database, where the jdbc code should be written in my application? Here am posting my application.. Please check it..

Thanks in Advance,
Lalitha

DetailsForm.java
------------
public class DetailsForm extends ActionForm{

// properties
String name;
int age;
String city;
String phone;

//setters and getters
}
FirstAction.java
---------

public class FirstAction extends Action{

public ActionForward execute(4parameters)

{

String forward = "firstpage";
if(request.getParameter("store") != null){
forward = "secondpage";
}

return mapping.findForward(forward);
}

}

SecondAction.java
-----------
public class SecondAction extends Action{

public ActionForward execute(4parameters)
{

String forward = "secondpage";
if(request.getParameter("submit") != null){
forward = "lastpage";
}
return mapping.findForward(forward);
}
}

LastAction.java
--------
public class LastAction extends Action{

public ActionForward execute(4parameters)
{

return mapping.findForward("lastpage");

}

}



firstjsp.jsp
---------
// code to create a form that contains name and age fields n submit button

<html:form action="/firstpage" method="post">

<html:text property="name" />

<html:password property="age" />

<html:submit property="store"/> <html:reset/>

secondjsp.jsp
-------

<html:form action="/secondpage" method="post">

<html:text property="city" />

<html:password property="phone" />

<html:hidden property="name" />

<html:hidden property="age" />

<html:submit property="submit"/>


lastjsp.jsp
---------

// just displaying the user entered data

Name = <bean:write name="detailsForm" property="name" />
Age = <bean:write name="detailsForm" property="age" />
City = <bean:write name="detailsForm" property="city" />
Phone = <bean:write name="detailsForm" property="phone" />


struts-config.xml
---------------
<action-mappings>
<action input="/firstjsp.jsp" path="/firstpage" name="detailsForm" type="example.actionclasses.FirstAction" scope="request">
<forward name="firstpage" path="/firstjsp.jsp" />
<forward name="secondpage" path="/secondjsp.jsp" />
</action>
<action input="/secondjsp.jsp" path="/secondpage" name="detailsForm" type="example.actionclasses.SecondAction" scope="request">
<forward name="secondpage" path="/secondjsp.jsp" />
<forward name="lastpage" path="/lastjsp.jsp" />
</action>
<action input="/lastjsp.jsp" path="/lastpage" name="detailsForm" type="example.actionclasses.LastAction" scope="request">
<forward name="lastpage" path="/lastjsp.jsp" />
</action>
</action-mappings>




Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

The Struts Documentation details the best practices on How To Access A Database.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Vidya Gupta
Ranch Hand

Joined: Mar 18, 2012
Posts: 98

Actually i know the code to be written to access a database.. but my problem is, in my application i tried by writing the code in LastAction.java class, but it is not storing the details in db..
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Lalitha Kotha wrote: but my problem is,..


How was I to know that from your original post? Have a look at our FAQ, HowToAskQuestionsOnJavaRanch. The better question you ask, the more help we can be.
Are you sure that LastAction is ever called?
 
Don't get me started about those stupid light bulbs.
 
subject: How to store the data entered by user in a form into a database using struts