Am trying to access a mysql database using struts ,but I get a error mgs:
error
exception
javax.servlet.ServletException: java.lang.ClassCastException: org.apache.commons.dbcp.DelegatingStatement
root cause
java.lang.ClassCastException: org.apache.commons.dbcp.DelegatingStatement.
why?
part code action class
-------------------------------------------------
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;
java.sql.Connection myConnection = null;
try {
dataSource = getDataSource(request);
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
rs.close();
stmt.close();
}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);
}
part code form class
-----------------------------------
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;
}
part code struts config
-----------------------------------------
<struts-config>
<data-sources>
<data-source type="org.apache.commons.dbcp.BasicDataSource">
<set-property
property="driverClassName"
value="com.mysql.jdbc.Driver" />
<set-property
property="url"
value="jdbc:mysql://localhost:3306/DBstruts" />
<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.DbStruts"
name="loginForm"
scope="request"
validate="true"
input="/login.jsp">
<forward name="success" path="/success.jsp"/>
<forward name="fail" path="/fail.jsp" />
</action>