aspose file tools*
The moose likes Struts and the fly likes Standard way of  connecting database in struts... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Standard way of  connecting database in struts... " Watch "Standard way of  connecting database in struts... " New topic
Author

Standard way of connecting database in struts...

veesam sridhar
Ranch Hand

Joined: Oct 18, 2005
Posts: 51
Hi guys,

which is the standard way of connecting to database in struts,
I read some where that using datasource in strutsconfig.xml is a deprecated process.

someone please explain me :

I am confused a little bit,

We should not write business logic in action serveltes, then we should write model classes separatly ,
1)how to connect database from model classes......
3)where should we write these classes (in the same directory where we r writing action classes?)
2)how to access model classes from actionservelts.....

someone please explain me or give me some usefull links which clears my doubt.
I already searched in old posts in this forum, but did not get the clarification.

Thnaks in advance,
Sridhar Veesam
[ July 03, 2006: Message edited by: veesam sridhar ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
1)how to connect database from model classes

You do this by creating a DataSource using the whatever method your J2EE Application Server recommends. Consult the documentation for your specific App Server for instructions on how to do it. In your code, you then use Java's JNDI (Java Naming and Directory Interface) to lookup the DataSource and use it to get a JDBC connection. This linkExplains more about how to do this.
2)where should we write these classes (in the same directory where we r writing action classes?)
It is generally considered best practice to put this classes in a different package from your action classes. The idea here is to create model objects that do not have dependencies on view or controller objects. That way, if you change to a different view, you can simply plug the model into a different view.

One of the reasons the Struts DataSource API was deprecated is that it kind of forced you to put code to get the DataSource in your Action class, and that's not where such code belongs. It belongs in your model objects.
2)how to access model classes from actionservelts.....

You do this the same way you do in any Java Classes.. you instantiate the class you want to use and call methods on it. So, if you create a class called CustomerDataAccessObject with a getCustomers() method, to use this object in your action class, just instantiate it and call its getCustomers() method.

You might also want to look into using the Data Access Object (DAO) Patternwhen creating classes that access data.


Merrill
Consultant, Sima Solutions
veesam sridhar
Ranch Hand

Joined: Oct 18, 2005
Posts: 51
Thankyou for your quick response..
I got the point ,
I will work on it to get it done.


Sridhar Veesam
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Standard way of connecting database in struts...