File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes database connection with struts! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "database connection with struts!" Watch "database connection with struts!" New topic
Author

database connection with struts!

Himal Chuli
Ranch Hand

Joined: Aug 06, 2002
Posts: 118
hi folks,
i am a novice about struts application and trying to connect a mysql database from struts application. i would appreciate if someone could teach me how to configure it so that i can access database from struts. even any online link would be a big help.
thanks.


Himal
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
You may configure your data sources in either the web.xml file (the same way you would with any servlet) or in the struts-config.xml, which has the same syntax as doing it in web.xml. I prefer to do it in web.xml since it is not necessarily a struts specific configuration.
A general recommendation is not to access the database directly from the struts action classes, but to instead develop a business model and access the database from there.


Matthew Phillips
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
I strongly agree with Mathew.
Both problem spaces database and struts should be loosely coupled. So your question is too global to answer, at least for me.
Struts is a framework which makes some things easier in web presentation layer (MVC, internationalization), whereas database should be accessed in business layer or persistence layer.
There is a book at O'Reilly which treats both topics in a concise manner with the main focus on struts. Its available over O-Reilly Safari (online access for 5 books for 10$ a month).
http://www.oreillynet.com/search/index.ncsp?sp-q=struts&sp-k=all
Found another one: This should be with a database, I guess. Haven't checked it personally, but looks interesting. For sure with database:
http://www.scioworks.com/jpetstore/jpetstore-camino.html
Himal Chuli
Ranch Hand

Joined: Aug 06, 2002
Posts: 118
hi mathew and axel,
thank you for you response. while web.xml can be configured to access database, i am also trying to get it done with configuring in struts-config.xml (setting tags like <datasources>,<datasource>...etc). that's what i am wondering if you guys could help me with.
currently i have my struts-config.xml set up like following:

but for some reason, it doesn't work."test" is the table i trying to connect with that reside in mysql database.i would appreciate if you guide me in this regard.
happy new year 2003.
thanks.
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
A couple of things that I noticed right off the bat:
1. Is test the name of the table or database? You should have the name of the database in your url.
2. The MySQL server runs on port 3306 by default. Have you changed that? If not, then you need to include a colon and the port number right after localhost.
You may also have to set a user property and a password property if the database has a user name and password. Your syntax appears to be correct.
Himal Chuli
Ranch Hand

Joined: Aug 06, 2002
Posts: 118
hi mathew,
thanks for your response again. yes, i put the port like this <set-property property="url" value="jdbc:mysql://localhost:3306/test". test is a database i wanted to connect with which has a test table. when i hit the submit button that is supposed to get another page with information from database, gives me an error like this:

i still don't know why. please help me with it.
thank.s
Jo Friedrich
Greenhorn

Joined: Dec 22, 2002
Posts: 6
A good example using mysql database is shown in the artimus application. It is explained in the book "Struts in Action".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: database connection with struts!