GeeCON Prague 2014*
The moose likes Struts and the fly likes Problem in Connecting to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem in Connecting to database" Watch "Problem in Connecting to database" New topic
Author

Problem in Connecting to database

Shyam Kasthala
Ranch Hand

Joined: Mar 30, 2006
Posts: 48
Hi all,
I am facing a problem in connecting to the database(Postgresql).I have given the following in my struts-config.xml file :

<data-sources>
<data-source>
<set-property property="description" value="PostgreSQL Data Source"/>
<set-property property="driverClass" value="org.postgresql.Driver" />
<set-property property="username" value="shyam"/>
<set-property property="password" value="shyam"/>
<set-property property="url" value="jdbc ostgresql://192.168.15.105:5432/MGS"/>
</data-source>
</data-sources>

I am getting the following error :

javax.servlet.ServletException: Servlet.init() for servlet action threw exception
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
java.lang.Thread.run(Unknown Source)

Thanks in advance.


Thanks & Regards<br /> Shyam Kasthala<br />ILFS Infotech Limited
Shyam Kasthala
Ranch Hand

Joined: Mar 30, 2006
Posts: 48
Sorry, I am getting the following error also :


java.lang.NoClassDefFoundError: org/apache/struts/legacy/GenericDataSource
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1629)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:850)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1299)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1181)
org.apache.struts.util.RequestUtils.applicationClass(RequestUtils.java:207)
org.apache.struts.util.RequestUtils.applicationInstance(RequestUtils.java:231)
org.apache.struts.action.ActionServlet.initModuleDataSources(ActionServlet.java:1079)
org.apache.struts.action.ActionServlet.init(ActionServlet.java:471)
javax.servlet.GenericServlet.init(GenericServlet.java:211)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
java.lang.Thread.run(Unknown Source)
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Please read the following thread:

http://www.coderanch.com/t/54917/Struts/Database-connection-method


Merrill
Consultant, Sima Solutions
Seetesh Hindlekar
Ranch Hand

Joined: Feb 13, 2004
Posts: 244
Hello,

Do you have the jar called struts-legacy.jar in the lib folder?
If yes, the add the same entry in the build.xml as

<path id="compile.classpath">
<pathelement path ="lib/struts-legacy.jar"/>
...
</path>

Rgds,

Seetesh
Shyam Kasthala
Ranch Hand

Joined: Mar 30, 2006
Posts: 48
Thanks for your reply.Now i got another problem.The following is my Action Class :


package app;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.util.*;
import javax.sql.*;
import java.sql.*;
import java.io.*;
import org.apache.commons.dbcp.BasicDataSource;
public class LoginAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response) throws Exception
{
DataSource dataSource;
Connection con = null;
try
{
dataSource = getDataSource(request);
con = dataSource.getConnection();
Statement st=con.createStatement();
ResultSet rs = st.executeQuery("select * from mgsch.login");
System.out.println("******************************************");
System.out.println("********Out Put from Login DataSource ******");
while(rs.next())
{
System.out.println("User Name is: " + rs.getString("user_name"));
}
System.out.println("******************************************");
rs.close();
st.close();
}
catch (SQLException sqle)
{
getServlet().log("Connection.process", sqle);
}
finally
{
try
{
con.close();
}
catch (SQLException e)
{
getServlet().log("Connection.close", e);
}
}
return mapping.findForward("success");
}
}

But it is not compiling.The error is :
Cannot resolve symbol
dataSource = getDataSource(request);

I have imported import javax.sql.*; and import org.apache.commons.dbcp.BasicDataSource; tto.
Please tell me where i am going wrong.My struts-config.xml is the same as above.Thanks in advance.
 
GeeCON Prague 2014
 
subject: Problem in Connecting to database