• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Standard way of connecting database in struts...

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
veesam sridhar
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou for your quick response..
I got the point ,
I will work on it to get it done.


Sridhar Veesam
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic