aspose file tools*
The moose likes Struts and the fly likes java.lang.String cannot be cast? need retrive data from 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 "java.lang.String cannot be cast? need retrive data from database" Watch "java.lang.String cannot be cast? need retrive data from database" New topic
Author

java.lang.String cannot be cast? need retrive data from database

ankit iit patel
Greenhorn

Joined: Jul 12, 2011
Posts: 3
my jsp page for find is like

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<html:html lang="true">
<body style="background-color: white">
<html:form action="/find" method="post">
First Name<html:text property="firstName"/>

<html:submit value="Find"/>
</html:form>
</body>
</html:html>

and success like
<jsp:useBean id="find" class="com.myapp.struts.DeleteAction"/>
<jsp:setProperty name="find" property="*"/>
<html:html>
<body style="background-color: white">

First Name<jsp:getProperty name="find" property="<%=request.getAttribute("firstName")%>"/>
Last Name <jsp:getProperty name="find" property="lastName"/>

</body>
</html:html>

java Action calss
public class DeleteAction extends org.apache.struts.action.Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String url = "jdbc:mysql://localhost:3306/";
String driverName = "com.mysql.jdbc.Driver";
String dataBase = "emp";
String userName = "root";
String password = "root";
Connection connection = null;
// Statement statement=null;
PreparedStatement prst=null;
ArrayList Data=new ArrayList();
try {
//Connection and driver

Class.forName(driverName);
connection = DriverManager.getConnection(url + dataBase, userName, password);
connection.setAutoCommit(true);

} catch (Exception e) {
System.out.println(e.getMessage());
}
//get data from form calss
DeleteActionForm deleteForm=(DeleteActionForm)form;
String FirstName = deleteForm.getFirstName();
// Statement stmt=connection.createStatement();
//sql quesry
prst=connection.prepareCall("select * from emp.delete where firstName='"+FirstName+"'");
ResultSet resultget = prst.executeQuery() ;
while(resultget.next()){
request.setAttribute("firstName",resultget.getString(1));
request.setAttribute("lastName",resultget.getString(2));
}
if(resultget==null){
return (mapping.findForward("success"));
}else{
return (mapping.findForward("fail"));
}

}}



i dnot knw where i make mistake

i got error like

org.apache.jasper.JasperException: java.lang.ClassCastException: java.lang.String cannot be cast to com.myapp.struts.DeleteActionForm


i try to change bean calss name=:deleteAction"
beacuse it my value is at request.setAttribute("firstName",resultget.getString(1));

but got above error

plase help me....

i am doing my project...last 2 week i am at this point.....i dnt knw how i slove
Rajagopal Mani
Ranch Hand

Joined: Mar 24, 2011
Posts: 84

Hi,

Welcome to Java Ranch.

The DeleteAction is not a java bean class. The First & Last name can be obtained without using <jsp:useBean> tag as below.

Using normal JSP Expression


Using JSTL


Hope this may help you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.String cannot be cast? need retrive data from database