This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Struts and the fly likes Database connection method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Database connection method" Watch "Database connection method" New topic

Database connection method

A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Hi all after getting all of the otherthings that I have been working on to work I am now facing the task of pulling in live data from the database. I can make a viable connection within the action form, but that just strikes me of bad design. I am trying to find out the best manner in which to create a connection to the database. I am currently using the BasicDataSource as it is defined in the struts-config.xml, but wanted to know if there is a better way of doing this (the reason I ask is the only way that I have found to get a connection in my database classes is to pass in the request object so that I can pull out the datasource - can someone point me to a good reference on current manners of connecting to a db.

Thanks once again for all your help on the matter.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I would strongly discourage you from using the Struts datasource utility. The best practice for all J2EE applications is to set up the DataSource as a JNDI resource using your application server's DataSource setup interface and then to lookup the datasource in JNDI in your applicaiton code.

You might also consider using the Data Access Object (DAO) pattern for your data access. This is one of the core J2EE patterns defined by Sun.

If you're using Tomcat, this link explains how to set up DataSources. If you're using some other application server, consult the documentation specific to the server you're using.

Consultant, Sima Solutions
A knibbs
Ranch Hand

Joined: Aug 23, 2006
Posts: 158
Thanks that is what I was looking for, for some reason JNDI completely escaped my mind.
I agree. Here's the link:
subject: Database connection method
It's not a secret anymore!