File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "connecting struts with database" Watch "connecting struts with database" New topic
Author

connecting struts with database

leo oke
Greenhorn

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">
<tr>
<td colspan="2">

</td>
</tr>
<tr>
<td width="15%">Enter your name:</td>
<td width="85%">
<html:text property="name" size="25" maxlength="50"
onfocus="this.select()"/>
</td>
</tr>

Formbean code

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

// Set values to the parameters
bean.setName(RS.getString("username"));

// extract data from the ResultSet
while(RS.next()){
String role = RS.getString(1);
if(role.equalsIgnoreCase("admin")||(role.equalsIgnoreCase("user")) ){
return mapping.findForward(SUCCESS);
}else{
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.


Merrill
Consultant, Sima Solutions
Jibitesh Prasad
Greenhorn

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