This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Problem in Connecting to database

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Shyam Kasthala
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please read the following thread:

https://coderanch.com/t/54917/Struts/Database-connection-method
 
Ranch Hand
Posts: 244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The two armies met. But instead of battle, they decided to eat some pie and contemplate this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic