aspose file tools*
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
Author

Connecting MySQL thru Struts

ketan kumar shah
Greenhorn

Joined: Jun 15, 2005
Posts: 1
Hi..

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
java.lang.NullPointerException
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
PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

<data-sources>
<data-source>
<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"/>
</data-source>
</data-sources>

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

<action-mappings>

<action path="/Lookup"
type="wiley.LookupAction"
name="lookupForm"
validate="true"
input="/index.jsp">
<forward name="success" path="/quote.jsp"/>
<forward name="failure" path="/index.jsp"/>
</action>

</action-mappings>
<controller processorClass="wiley.WileyRequestProcessor" />
<message-resources parameter="wiley.ApplicationResources"/>
</struts-config>


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:
http://struts.apache.org/faqs/database.html

My apologies if you have already considered this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connecting MySQL thru Struts
 
Similar Threads
Cannot find ActionMappings or ActionFormBeans collection error
Data Source trouble - javax.servlet.UnavailableException: Initializing applic (SOLVED)
Struts: DB Connection
HTTP Status 503 - Servlet action is currently unavailable
errors occured...for database ..my god~kick my head~