connectivity problem

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;

import java.sql.*;

public class TestDataSource extends Action

{

public ActionForward execute(

ActionMapping mapping,

ActionForm form,

HttpServletRequest request,

HttpServletResponse response) throws Exception{

javax.sql.DataSource dataSource;

java.sql.Connection myConnection=null;

try {

dataSource = getDataSource(request);

myConnection = dataSource.getConnection();

Statement stmt=myConnection.createStatement();

ResultSet rst=stmt.executeQuery("select username from test");

System.out.println("******************************************");

System.out.println("********Out Put from TestDataSource ******");

while(rst.next()){

System.out.println("User Name is: " + rst.getString("username"));

}

System.out.println("******************************************");

rst.close();

stmt.close();

// do what you wish with myConnection

} catch (SQLException sqle) {

getServlet().log("Connection.process", sqle);

} finally {

//enclose this in a finally block to make

//sure the connection is closed

try {

myConnection.close();

} catch (SQLException e) {

getServlet().log("Connection.close", e);

}

}

return mapping.findForward("success");

}

}

this is the connectivity program of strut with MYSQL. it gives error while

compiling the program. i set strut.jar, servlet.jar. mysqlconnector.jar file before compiling.

it gives this type error.

TestDataSource.java:23: cannot resolve symbol

symbol : method getDataSource (javax.servlet.http.HttpServletRequest)

location: class TestDataSource

dataSource = getDataSource(request);

^

1 error

pls solve my problem

[2144 byte] By [Java_Jsp_servleta] at [2007-11-27 9:05:49]
# 1
I suggest you enclose your code with the proper 'code' tags to make it more readable. ;)
lem@phila at 2007-7-12 21:40:31 > top of Java-index,Java Essentials,New To Java...
# 2
Sorry ignore this postMessage was edited by: diptaPB
diptaPBa at 2007-7-12 21:40:31 > top of Java-index,Java Essentials,New To Java...
# 3
... And I've checked the javax.sql package and found out that DataSource is an interface, and not a class.
lem@phila at 2007-7-12 21:40:31 > top of Java-index,Java Essentials,New To Java...
# 4

> ... And I've checked the javax.sql package and

> found out that DataSource is an interface, and

> not a class.

That should not matter.. as getDataSource returns an object of type javax.sql.DataSource.

To the OP

As i see the java API for Action class there is a method getDataSource. But when i tried to run your code I found that the method is not present in the Action class of the struts library. I hope this is some version mismatch of the struts library. As a result your class is not extending the method from the base class.

I found that if you use struts 2.0.8 (I hope you are using it since it is the latest) you will face this problem because in this the lib the Action class does not contain the getDataSource method. But if you use old struts (I checked with 1.2.7 available at http://archive.apache.org/dist/struts/struts-1.2.7/struts-1.2.7-lib.zip) this problem will not occure as in this libraries the Action class contains the getDataSource method. This is a peculiar problem as the newer version has removed the method from the class. This is not correct as I think.

By the way if you use the struts 1.2.7 (available at the above url) then your problem will be solved.

Hope it helps :)

Message was edited by:

diptaPB

Message was edited by:

diptaPB

diptaPBa at 2007-7-12 21:40:31 > top of Java-index,Java Essentials,New To Java...