File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes connecting struts with database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "connecting struts with database" Watch "connecting struts with database" New topic

connecting struts with database

leo oke

Joined: Jul 09, 2007
Posts: 21
I want to do this, When the user input is processed, it will be checked against the database to see what�s the role associated with the user. If the user is in the �admin� role, it will show �Hello Admin� on the second page.
Using struts and mysql, I've got the jsp,Formbean ,
The problem is: The requested resource (Servlet action is not available) is not available.

part code

<html:form action="/login">
<table width="100%" border="0" cellspacing="2" cellpadding="0">
<td colspan="2">

<td width="15%">Enter your name:</td>
<td width="85%">
<html:text property="name" size="25" maxlength="50"

Formbean code

public class LoginForm extends org.apache.struts.validator.ValidatorForm{

private String name = null;

private String result ;

public void setName(String 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();

//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 = ?";
ResultSet RS=stmt.executeQuery(query);

// Set values to the parameters

// extract data from the ResultSet
String role = RS.getString(1);
if(role.equalsIgnoreCase("admin")||(role.equalsIgnoreCase("user")) ){
return mapping.findForward(SUCCESS);
return mapping.findForward("fail");


}catch (SQLException sqle) {


return mapping.findForward(SUCCESS);
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The problem appears to be that the Struts ActionServlet didn't initialize properly. See this link for more information.

Consultant, Sima Solutions
Jibitesh Prasad

Joined: Feb 20, 2007
Posts: 20
have you specified the DataSource in the struts-config.xml file??
I agree. Here's the link:
subject: connecting struts with database