Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
ankit iit patel
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 84
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic