• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Struts and database connection

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need help to connect to mysql database. The problem is: The requested resource (Servlet action is not available) is not available.

part codepublic class LoginForm extends org.apache.struts.validator.ValidatorForm{

private String name = null;

private String result ;

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}



public void setResult(String result) {
this.result = result;
}

public String getResult() {
return result;
}

action code
public class DB extends Action {

/* forward name="success" path="" */
private final static String SUCCESS = "success";


public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

//casting a formbean

LoginForm bean =(LoginForm) form ;

//get form parameters
String name = bean.getName();
try{


//get connection from database
javax.sql.DataSource dataSource = null;
java.sql.Connection myConnection;

ServletContext context=servlet.getServletContext();
DataSource datasource;
myConnection = dataSource.getConnection();

PreparedStatement stmt=(PreparedStatement) myConnection.createStatement();

// Execute a query

String query = "SELECT * FROM `role` NATURAL JOIN `user` WHERE username = '"+bean.getName()+"' ";
ResultSet RS=stmt.executeQuery(query);

// extract data from the ResultSet
while(RS.next()){
String role = RS.getString("role");

if(role.equalsIgnoreCase("admin")||(role.equalsIgnoreCase("user")) ){

HttpSession session = request.getSession();
session.setAttribute("result",role);
return mapping.findForward("success");

}else{
return mapping.findForward("fail");
}

}

//close connection
myConnection.close();
RS.close();
stmt.close();
...................................................................

part code

<struts-config>

<data-sources>
<data-source type="org.apache.tomcat.dbcp.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="com.mysql.jdbc.Driver" />
<set-property
property="url"
value="jdbc:mysql://localhost:3306/DBstruts?autoReconnect=true" />
<set-property
property="username"
value="root" />
<set-property
property="password"
value="" />
<set-property
property="maxActive"
value="10" />
<set-property
property="maxWait"
value="5000" />
<set-property
property="defaultAutoCommit"
value="false" />
<set-property
property="defaultReadOnly"
value="false" />
<set-property
property="validationQuery"
value="SELECT COUNT(*) FROM role" />
</data-source>
</data-sources>
<form-beans>
<form-bean name="loginForm"
type="com.jjolt.jjlogin.LoginForm">
</form-bean>
</form-beans>

<global-exceptions>

</global-exceptions>

<global-forwards>
<forward name="login" path="/login.jsp"/>

</global-forwards>

<action-mappings>

<action path="/login"
type="com.jjolt.jjlogin.DB"
name="loginForm"
scope="request"
validate="true"
input="/login.jsp">
<forward name="success" path="/success.jsp"/>
<forward name="fail" path="/fail.jsp" />

</action>

</action-mappings>

<controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>

<message-resources parameter="com/myapp/struts/ApplicationResource"/>

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems your struts-config file can't find your DB action class. It has nothing to do with the database connection. Check the packages and paths in the config file.

Did you configure the ActionServlet in web.xml?
[ July 24, 2007: Message edited by: Manuel Leiria ]
 
author & internet detective
Posts: 39959
804
Eclipse IDE VI Editor Java
 
I'm full of tinier men! And a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic