File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Standard way of connecting database in struts...

 
veesam sridhar
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic