Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem in Struts Application while connecting to database

 
nanda gopal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to struts application, while i am connecting my application to database, using this code
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection("jdbc dbc:StrutsData");
-its' work fine , but when i am using struts-config.xml data source code tomcat display following error and i get one more error message in my Action class that is
datasource = getDataSource(request); (in this line i show The method getDataSource(HttpServletRequest) is undefined for the type LoginAction) error , so anybody please help me..

May 30, 2007 2:33:51 PM org.apache.struts.action.ActionServlet initApplicationDa
taSources
SEVERE: Initializing application data source org.apache.struts.action.DATA_SOURC
E
java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoa
der.java:1406)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoa
der.java:1254)
at org.apache.struts.util.RequestUtils.applicationClass(RequestUtils.jav
a:191)
at org.apache.struts.util.RequestUtils.applicationInstance(RequestUtils.
java:217)
at org.apache.struts.action.ActionServlet.initApplicationDataSources(Act
ionServlet.java:911)
at org.apache.struts.action.ActionServlet.init(ActionServlet.java:446)
at javax.servlet.GenericServlet.init(GenericServlet.java:258)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.
java:918)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:81
0)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContex
t.java:3279)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3
421)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:78
5)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:478)

at org.apache.catalina.core.StandardHost.install(StandardHost.java:738)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:300
)
at org.apache.catalina.startup.HostConfig.start(HostConfig.java:389)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java
:232)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(Lifecycl
eSupport.java:155)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1131)

at org.apache.catalina.core.StandardHost.start(StandardHost.java:638)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1123)

at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:343
)
at org.apache.catalina.core.StandardService.start(StandardService.java:3
88)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:506
)
at org.apache.catalina.startup.Catalina.start(Catalina.java:781)
at org.apache.catalina.startup.Catalina.execute(Catalina.java:681)
at org.apache.catalina.startup.Catalina.process(Catalina.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:243)
Starting service Tomcat-Apache
Apache Tomcat/4.0.4

<?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">

my sturts-config.xml file is
<struts-config>
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property property="driverClassName" value="sun.jdbc.odbc.JdbcOdbcDriver"/>
<set-property property="url" value="jdbc dbc:StrutsData"/>
</data-source>
</data-sources>
<form-beans>
<form-bean name="loginForm" type="LoginForm"/>
</form-beans>

<global-exceptions>

</global-exceptions>

<global-forwards>

</global-forwards>

<action-mappings>

<action path="/login" type="LoginAction" scope="request" name="loginForm" input="/WebPage/Login.jsp">
<forward name="success" path="/WebPage/Success.html" redirect="true"/>
<forward name="failure" path="/WebPage/Failure.html" redirect="true"/>
</action>

</action-mappings>

<controller
contentType="text/html;charset=UTF-8"
debug="3"
locale="true"
nocache="true"/>

<message-resources parameter="BankingMessageResources" null="false"/>
</struts-config>

my Action class is

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.*;
import javax.sql.*;
public class LoginAction extends Action {
DataSource datasource = null;
Connection connection = null;
Statement statement = null;
int i=0;

public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {

LoginForm lf = (LoginForm)form;
String username = lf.getUsername();
String password1 = lf.getPassword1();
String password2 = lf.getPassword2();
String query = "insert into tbluser(username,password1,password2) values('"+username+"','"+password1+"','"+password2+"')";
if(password1.equals(password2)) {
try {
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//connection = DriverManager.getConnection("jdbc dbc:StrutsData");
datasource = getDataSource(request);
connection = datasource.getConnection();
}catch(Exception e) {
System.out.println("Error in Driver Connection");
}
try {
statement = connection.createStatement();
i = statement.executeUpdate(query);
System.out.println("Query Executed i= "+i);
return mapping.findForward("success");

}catch(Exception e) {

}
return mapping.findForward("failure");
}
return mapping.findForward("failure");
}

}
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Struts DataSource utility has been deprecated, so you simply won't find the getDataSource method in the Action class any more. That's why you're getting the error.

For some information on what to use instead, see question 7 of the Javaranch Struts FAQ
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic