• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Connecting MySQL thru Struts

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 96
Scala VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic