jQuery in Action, 3rd edition
The moose likes Struts and the fly likes Connecting MySQL thru Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Connecting MySQL thru Struts" Watch "Connecting MySQL thru Struts" New topic

Connecting MySQL thru Struts

ketan kumar shah

Joined: Jun 15, 2005
Posts: 1

I'm trying to connect MySQL thru Struts config on Tomcat 4.1 but I get the error while trying to load the application into the server.

The error is..

org.apache.struts.action.ActionServlet initModuleDataSources
SEVERE: Initializing application data source org.apache.struts.action.DATA_SOURCE
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1355)

I've configured the database in struts config file as

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"


<set-property property="driverClass" value="org.gjt.mm.mysql.Driver" />
<set-property property="url" value="jdbc:mysql://localhost/stocks" />
<set-property property="maxCount" value="5"/>
<set-property property="minCount" value="1"/>
<set-property property="user" value="root"/>
<set-property property="password" value="admin"/>

<form-bean name="lookupForm" type="wiley.LookupForm"/>


<action path="/Lookup"
<forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/index.jsp"/>

<controller processorClass="wiley.WileyRequestProcessor" />
<message-resources parameter="wiley.ApplicationResources"/>

But if I comment out the <data-sources> part and prepare the connection in Action class then it works

DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
conn = DriverManager.getConnection("jdbc:mysql://localhost/stocks","root","admin");

as I get the queries executed successfully.

And also if I use the following code to get Data Source

DataSource dataSource = (DataSource) servlet.getServletContext().getAttribute(Action.DATA_SOURCE_KEY);

I get the compile error Action.DATA_SOURCE_KEY can not be resolved.

I will need this to be resolved too after able to connect to the database thru struts config.

Please help me out to resolve the problem.
Samuel Cox
Ranch Hand

Joined: Nov 16, 2004
Posts: 96

Try specifying the type, ie:

<data-source type="org.apache.commons.dbcp.BasicDataSource">
Samuel Cox
Ranch Hand

Joined: Nov 16, 2004
Posts: 96

Btw, unless this is a small project or you have no flexibility. I would strongly considering decoupling your persistence layer from Struts. Our webapp accesses our databases exactly like you are going to do, and we are finally realizing our mistake and now need to push the persistence layer behind the business logic.

See this article:

My apologies if you have already considered this.
I agree. Here's the link: http://aspose.com/file-tools
subject: Connecting MySQL thru Struts
It's not a secret anymore!