File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes java.lang.ClassCastException 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 » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "java.lang.ClassCastException" Watch "java.lang.ClassCastException" New topic
Author

java.lang.ClassCastException

AmitCdac Grover
Greenhorn

Joined: Mar 30, 2005
Posts: 26
hi...

i have made bmp bean when i try to run the following method(ejbFindByFirstName()) from the client side it will give me the following error.here i am sending u client code ,menthod and a class which i am using

------------------
java.lang.ClassCastException: EmpEntityBean_xzbwyf_EOImpl_WLStub
at ConnectClient.go(ConnectClient.java:50)
at ConnectClient.main(ConnectClient.java:13)
---------------
public Collection ejbFindByFirstName(String lFirstName) throws FinderException
{
PreparedStatement pstmt = null;
Connection conn = null;
ResultSet result = null;
ReturnVector rv=new ReturnVector();

try
{
conn = this.getConnection();
System.out.println("ejbFindByFirstName(" + lFirstName + ") called");

pstmt = conn.prepareStatement("SELECT EMPID,EMPFIRSTNAME,EMPLASTNAME FROM EMPDATA WHERE EMPFIRSTNAME = ?");

pstmt.setString(1, lFirstName);
result = pstmt.executeQuery();
Vector keys=new Vector();

System.out.println("Before while in findbyfirst name");
while (result.next())
{
System.out.println("in while findbyfirst name");
System.out.println(result.getInt("EMPID"));
System.out.println(result.getString("EMPFIRSTNAME"));
System.out.println(result.getString("EMPLASTNAME"));

rv.EmpId=result.getInt("EMPID");
rv.FirstName=result.getString("EMPFIRSTNAME");
rv.LastName=result.getString("EMPLASTNAME");
keys.addElement(rv);
}
System.out.println("After while in findbyfirst name");
return keys;
}
catch (Exception e)
{
throw new FinderException(e.toString());
}
finally
{
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) {}
try { if (conn != null) conn.close(); }
catch (Exception e) {}
}

}

----------------------
import java.io.Serializable;
public class ReturnVector implements java.io.Serializable
{
int EmpId;
String FirstName;
String LastName;
}
---------------------
client code
--------------------------
Collection c = home.findByFirstName("hello");

Vector v=new Vector(c);;
ReturnVector rv=(ReturnVector)v.firstElement(); // error is here
System.out.println(rv.EmpId);
System.out.println(rv.FirstName);
System.out.println(rv.LastName);

-------------------------

my ejbFindByFirstName() method returning a vector which contain the object of class ReturnVector. i used this class be becoz i want to return multiple values form method.plz help me out how can i solve this problem. i also want to know that if my method returning multiple records how i can i get from the client side...


in my above problem in find method returning vector contain object of RetunVector class. if my returning vector contain string or int then from the client if i access then there is not any error but it will print following

EmpEntityBean_xzbwyf_EOImpl_WLStub

i didnot understand what this is all about

plz help me to solve this problem.....

regards
amit grover
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I would like to point you at Section 10.7.3 of the EJB 2.0 spec, which states:

The return type of a finder method must be the entity bean�s primary key type, or a collection of primary keys.


Which means that the collection you are returning does not contain the expected object types. Unless you declare ReturnVector as being the primary key class (10.6.13) of your bean, your code won't work...


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.ClassCastException