Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Connecting MySQL thru Struts

 
ketan kumar shah
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try specifying the type, ie:

<data-source type="org.apache.commons.dbcp.BasicDataSource">
 
Samuel Cox
Ranch Hand
Posts: 96
Scala Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic