一个Java的小问题(41分)

Z

zhfhq

Unregistered / Unconfirmed
GUEST, unregistred user!
大侠们,现在我在做时总是碰见这个小问题,就是抛出的一个异常:
java.lang.ClassCastException
而这个异常的得到共有以下几种情况:
比如我现在将一个子类加入到一个集合类中,等再取出时,我做了一个转换,就会出现这种情况
例如
父类 A
子类1 B
子类2 c

我将子类b,c放入集合f中
取出时:
d=(A)f.getelement();
出现异常
 
大侠们,回答吧,难道这个问题就这样的难吗?
<font color=red size =12>急急</font>
 
子类1 B 、子类2 c不是同一个类,只是属于同一个基类,如果是1b 和2c是同一个类,
则没有问题
 
to hicharlie:
我现在就是把不同的类放入到一个集合中,然后在取出来用,如果我现在是同一类,我就
不会做转换,就更本不回出问题了.
 
各位大侠:
我现在给大家更清楚的解释,我举一个例子,我现在发现是的错误:
我下面的这段代码,绝对是真确的(已经实验过):
int i=0;
while (i<11)
{
v.addElement(new Integer(i));
//addElementdo
esn't
i++;
}
/*
* Return an enumeration of found primary keys
*/
java.util.Enumeration e=v.elements();
while(e.hasMoreElements())
{
System.out.println("Elements:"+e.nextElement()+"/");
}
结果使这样的:
Elements:0/
Elements:1/
Elements:2/
Elements:3/
Elements:4/
Elements:5/
Elements:6/
Elements:7/
Elements:8/
Elements:9/
Elements:10/
可是我把这段代码作为EJB的远程方法的一部分时,就出现错误
假设有一个EJB的方法:
public java.util.Enumeration ejbFindByOwnerName(String name) throws
FinderException, RemoteException {
PreparedStatement pstmt = null;
Connection conn = null;
Vector v = new Vector();
try {
System.out.println("ejbFindByOwnerName(" + name + ") called");
/*
* Acquire DB connection
*/
conn = getConnection();
/*
* Find the primary keys in the DB
*/
pstmt = conn.prepareStatement(
"select id from accounts where ownerName = ?");
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
/*
* Insert every primary key found into a vector
*/
int i=0;
while (rs.next()) {
String id = rs.getString("id");
//AccountPK tempPK=new AccountPK(id);
//v.addElement(tempPK);
v.addElement(new Integer(i));
//addElementdo
esn't
i++;
}
在客户端调用这个方法时:
Context ctx = new InitialContext();
//look up jndi name
Object ref = ctx.lookup("Account");
//cast to Home interface
AccountHome home = (AccountHome) PortableRemoteObject.narrow(ref, AccountHome.class);
home.create("13-459967-8799","John Smith");
String name="John Smith";
java.util.Enumeration e=home.findByOwnerName("John Smith");
//account=(Account)home.findByPrimaryKey(new AccountPK("123-456-7899"));
//System.out.println("test zhf");
while(e.hasMoreElements())
{
System.out.println("Elements:"+e.nextElement()+"/");
}
在服务器段:
输出是真确的(如我前所述)
而在客户端出现下面的结果:
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(0)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(1)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(2)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(3)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(4)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(5)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(6)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(7)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(8)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(9)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(10)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(11)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(12)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(13)}],codebase=null]/
Elements:Stub[repository_id=RMI:bankentitybean.Account:0000000000000000,key=ServiceId[service=/EJB[ejbcontainer]Account,id={68 bytes: (127)(255)(255)(2)(0)(0)(0)[8][R][M][:][j][a][v][a][.][l][a][n][g][.][n][t][e][g][e][r][:][6][4][D][4][7][C][1][D][0][1][9][8][0][5][E][:][1][2][E][2][A][0][A][4][F][7][8][1][8][7][3][8](0)(0)(0)(0)(14)}],codebase=null]/
这是啥原因,这也是我以前说的异常产生的主要原因。

 
是: A d=(A)f.getelement()吗?
确信不是 B d=(A)f.getelement();?
 
to YoYoYo:
确信
 
好像是没有道理。
用 (e.nextElement() instanceof B)去查它的原类,
返回是true, 就用B去声明,
返回是false,就用A去声明。
 
To YoYoYo:
两个都不是,大哥,我现在用的是EJB,你可以看看我给你的例子,他给的结果就很不相同
 
这个问题就这样的难吗?难道就没有人会用吗?
 
结果是对的!
现在没时间,有空跟你说
 
:JJams_King
大侠,你一定要给我说呀,我快不行了,我已经几天没有编程序了。
 
:JJams_King
大侠,快帮帮忙了,我在次感谢你了
 
难道大家嫌分少,如果是这样,我在给你们加点分了,我很急的,这样的问题难道就没有人
回吗?
 
其实,在EJB体系中规定,客户是不可以直接接触到Bean的实现的,EJB客户都是通过
Remote接口得到一个客户视图,通过这个视图操作Bean的。容器在这中间架起客户到
Bean实现的桥梁,一方面通过实现Remote接口给客户提供客户视图,一方面分析客户
的请求,调用Bean的实现得到客户要的结果。
对于PrimaryKey,其主要作用是让容器可以identify一个实体Bean,而当客户通
过客户视图(Home和Remote接口)得到find结果时,容器已经将这些PrimaryKey换成相对应
的EJBObject了,所以你在客户端拿到的是EJBObject的引用。也就是容器给客户提供
的视图。拿到这些EJBObject后,你可以直接调用其方法,这时候一个Bean实现就会
给你提供服务。
 
To:JJams_King:
那我该如何的调用,才能够得到我想得到的预期结果呢?
 
不知道你预期什么结果
find返回的结果是EJBObject或EJBObject列表,每一个对应DB中一行。如果要取到这行
里的某个字段,应该增加相应的方法。(Remote接口和Bean实现)。
 
To JJams_King:
其实并不是find方法是这样的,其他的Remote接口中实现的方法都是这样的,现在我再举一个例子:
我在JB4定义了一个Container 管理的实体Bean,下面是起原代码:
package product;
import java.rmi.*;
import javax.ejb.*;
import java.util.*;
public interface ProductHome extends EJBHome
{
public Product create(String productID,String name,String description,double basePrice) throws RemoteException, CreateException;
public Product findByPrimaryKey(ProductPK Key) throws RemoteException, FinderException;
public Enumeration findByName(String name)throws FinderException,RemoteException;
public Enumeration findByDescription(String description)throws FinderException,RemoteException;
public Enumeration findByBasePrice(double basePrice)throws FinderException,RemoteException;
public Enumeration findExpensiveProducts(double minPrice)throws FinderException,RemoteException;
public Enumeration findCheapProducts(double maxPrice)throws FinderException,RemoteException;
public Enumeration findAllProducts() throws FinderException,RemoteException;
}
package product;
import java.rmi.*;
import javax.ejb.*;
public class ProductBean implements EntityBean
{
EntityContext entityContext;
public String productID;
// PK
public String name;
public String description;
publicdo
uble basePrice;
public ProductBean()
{
System.out.println("New Product Entity Bean Java Object created by EJB Container.");
}
public String getName() throws RemoteException
{
System.out.println("getName() called.");
return name;
}
public void setName(String name) throws RemoteException
{
System.out.println("getName() called.");
this.name = name;
}
public String getDescription() throws RemoteException
{
System.out.println("getDescription() called.");
return description;
}
public void setDescription(String description) throws RemoteException
{
System.out.println("setDescription() called.");
this.description = description;
}
publicdo
uble getBasePrice() throws RemoteException
{
System.out.println("getBasePrice() called.");
return basePrice;
}
public void setBasePrice(double price) throws RemoteException
{
System.out.println("setBasePrice() called.");
this.basePrice = price;
}
public String getProductID()
{
System.out.println("getProductID() called.");
return productID;
}
public ProductPK ejbCreate(String productID,String name,String description,double basePrice) throws CreateException
{
/**@todo: Implement this method*/
System.out.println("ejbCreate(" + productID + ", " + name + ", " +description + ", " + basePrice + ") called");
this.productID = productID;
this.name = name;
this.description = description;
this.basePrice = basePrice;
return null;
}
public void ejbPostCreate(String productID,String name,String description,double basePrice) throws CreateException
{
System.out.println("ejbPostCreate() called");
}
public void ejbRemove() throws RemoveException
{
System.out.println("ejbRemove() called.");
}
public void ejbActivate()
{
System.out.println("ejbActivate() called.");
}
public void ejbPassivate()
{
System.out.println("ejbPassivate () called.");
}
public void ejbLoad()
{
System.out.println("ejbLoad() called.");
}
public void ejbStore()
{
System.out.println("ejbStore() called.");
}
public void setEntityContext(EntityContext entityContext)
{
this.entityContext = entityContext;
}
public void unsetEntityContext()
{
entityContext = null;
}
}
package product;
import java.rmi.*;
import javax.ejb.*;
public interface Product extends EJBObject
{
public String getName() throws RemoteException;
public void setName(String name)throws RemoteException;
public String getDescription() throws RemoteException;
publicdo
uble getBasePrice() throws RemoteException;
public void setBasePrice(double price) throws RemoteException;
public String getProductID() throws RemoteException;
}
客户端是这样的:
package product;
import javax.ejb.*;
import javax.naming.*;
import java.rmi.*;
import javax.rmi.*;
import java.util.Enumeration;
public class Client
{
public Client()
{
}
public static void main(String[] args)
{
ProductHome home = null;
try {
/*
* Get a reference to the Product Home Object - the
* factory for Product EJB Objects
*/
Context ctx = new InitialContext();
//look up jndi name
Object ref = ctx.lookup("Product");
//cast to Home interface
home = (ProductHome) PortableRemoteObject.narrow(ref, ProductHome.class);
/*
* Use the factory to create the Product EJB Object
*/
home.create("123-456-7890", "P5-350", "350 Mhz Pentium", 200);
home.create("123-456-7891", "P5-400", "400 Mhz Pentium", 300);
home.create("123-456-7892", "P5-450", "450 Mhz Pentium", 400);
home.create("123-456-7893", "SD-64", "64 MB SDRAM", 50);
home.create("123-456-7894", "SD-128", "128 MB SDRAM", 100);
home.create("123-456-7895", "SD-256", "256 MB SDRAM", 200);
/*
* Find a Product, and print out its description
*/
Enumeration enum = home.findByName("SD-64");
System.out.println("The following product descriptions match the product name SD-64:");
while (enum.hasMoreElements())
{
Product prod = (Product) enum.nextElement();
System.out.println(prod.getDescription());
}
/*
* Find all products that cost $200
*/
System.out.println("Calling finder to find all products that cost $200");
enum = home.findByBasePrice(200);
while (enum.hasMoreElements())
{
Product prod = (Product) enum.nextElement();
System.out.println(prod.getDescription());
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if (home != null)
{
try
{
System.out.println("Destroying products..");
/*
* Find all the products
*/
Enumeration enum = home.findAllProducts();
while (enum.hasMoreElements())
{
try
{
Product prod = (Product) enum.nextElement();
prod.remove();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
在调用的过程中出现这样的错误:
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
出错的语句是:
Product prod = (Product) enum.nextElement();
请问这是为何?
 
呵呵,有点意思。看起来好像是忘了narrow的结果,可是这不该你做。
把异常贴出来吧
 
客户端的输出就是这样:
The following product descriptions match the product name SD-64:
Destroying products..
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:53)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
java.lang.ClassCastException: com.inprise.vbroker.rmi.CORBA.ObjectImpl
at product.Client.main(Client.java:86)
 
顶部