This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Struts and the fly likes Accessing Databases with Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Accessing Databases with Struts" Watch "Accessing Databases with Struts" New topic
Author

Accessing Databases with Struts

Gaurav Kshatriya
Ranch Hand

Joined: Mar 07, 2010
Posts: 33
Hi,

I am new to Struts. I am learning Struts by looking at the sample example. And so far i am able to understand clearly. But as there is no database example in them i am not able to use database. I am using Struts1.

Can anyone please send some consolidated database applications or if someone can teach me it would be fantastic.

Thanks
Gaurav


Thanks
Gaurav.... Cheers....
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

There's nothing special about accessing a database with Struts, unless you're using the deprecated <datasource> configuration element--which I wouldn't recommend.
Gaurav Kshatriya
Ranch Hand

Joined: Mar 07, 2010
Posts: 33
Hi David

Thanks for the immediate response. But i want to say that i dont know anything about how to access a database from struts 1. So i was asking for some code examples by which i can easily understand. If you can share it would be great help.

Thanks
Gaurav
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Again: search for any JDBC tutorial (if that's how you're doing it--you don't say if you're using Spring, Hibernate, etc.)

There's nothing Struts-specific about this.
Eswar Nec
Ranch Hand

Joined: May 02, 2010
Posts: 105
Hi,

Note: Each and every step before doing read the important notes and follow . I give important notes in bottom.

1. Open a NetBeans editor.

2. Select the following method for create empty project.

File -> NewProject -> Web -> WebApplication
Next -> Give project name -> select project location
Next -> Select struts (check box).
Finish.

3. Designing form inside the index.jsp

Whatever you need (textbox,button,label etc…)

4. Create a new package under source package

Right click SourcePackage -> Java Package -> Give the package name.

5. Create a new formbean file

RightClick JavaPackage -> new -> StrutActionFormBean
Give the class name

6. Create a new action file

RightClick JavaPackage -> new -> StrutAction
Give the classname , Actionpath ** (classname must be equal ActionPath)
Select Request or Session .

7. Create a new java class for DBConnect

Database connection establishment coding here.

8. Java Script

Create a new Folder for javascript under Webpages.
Rightclick javascriptfolder -> new -> File/Folder
Next -> Other folder -> EmptyFile
Next -> Give name. -> Finish.


Important Notes:

3.1 Form Creation

Give the form action Name
Create a textbox, password field, button .
Note the FormName and Property




5.1 Form bean creation

The formbean name must be equal to form name.

Egs. Formbean name is TestForm.

Then the following coding inside the FormBean(TestForm)
Declare the following variables
Variables names and property names are must be equal.



Select all the above variables and right click
Refractor -> Encapsulate Fields.
Select get and set username, password, submit -> Next -> Do Refractoring.

6.1 Strut Action creation

The strut action name and form action name must be equal.

Egs: Action name is TestAction.

Here only we will do the orginal coding .Get the form value , database manipulation etc…



Finally after complete the each and every file look up to the (strutconfig.xml) file.


Error will be occur the following situation:

1. Form name and Struts action form bean name are not match.

2. Form Action name and strut action name are not match.

3. Variable names and property names are not equal.

4. All package are properly not imported.(java and strut packages).

5. All taglib file are properly import into the designing file(*.jsp file)

6. Not proper entry for inside the strutconfig.xml file.

7. Iterate id name is equal to package Name. And In Iterate procedure bean write name is also equal to package name.


Remainig you will refer JDBC tutorial and struts tutorial.


Wake up! Don't let your smile be snatched away by anybody!
Regards, Eswar
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Eswara: What's the point of the big long example if it doesn't answer the only question that was asked?!?!?! And it's "Refactor", not "Refractor".
Gaurav Kshatriya
Ranch Hand

Joined: Mar 07, 2010
Posts: 33
@Eswara : Thank you for the example. I am trying to do it.

@David : I was looking for an example like this only where i can find where the JDBC code should be written. Like Eswara has told that in ActionForm it should be written.

So its still not completly clear in my mind but i am trying. If anyone has more inputs on the same then please share.

Thanks
Gaurav
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

See this thread on JavaRanch, specifically this link from the FAQ regarding pre-populating ActionForms.
Gaurav Kshatriya
Ranch Hand

Joined: Mar 07, 2010
Posts: 33
@David : Thank you for the links.

Regards
Gaurav
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing Databases with Struts