wood burning stoves 2.0*
The moose likes Struts and the fly likes Query about datasource Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Query about datasource" Watch "Query about datasource" New topic
Author

Query about datasource

Maheswaran Devaraj
Ranch Hand

Joined: Sep 14, 2007
Posts: 47
Hi all,
I configured the needed properties in my struts-config.xml. And I'm using the foolowing java code to access the database. but it shows yhe error in the following line

"dataSource = getDataSource(request);" The error is The method
getSource is undefined for the class TestDataSource. Any one help me to resolve this

the coding is following:
------------------------


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=null;
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");
}

thanks®ards,
Maheshwaran Devaraj
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

What version of Struts are you using? That method is available in 1.2.9 but not in 1.3.8 (the current GA version of Struts 1.x)


[How To Ask Questions On JavaRanch]
Maheswaran Devaraj
Ranch Hand

Joined: Sep 14, 2007
Posts: 47
How can we access it using struts1.3.8
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8997
    
    9

You cant.
The Struts FAQ: How to Access a Database explains why.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Query about datasource