This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query about datasource

 
Maheswaran Devaraj
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Maheswaran Devaraj
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can we access it using struts1.3.8
 
Joe Ess
Bartender
Pie
Posts: 9258
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cant.
The Struts FAQ: How to Access a Database explains why.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic