测试类SessionClient的源码如下: 
/* 
* Created on 2004-10-28 

* To change the template for this generated file go to 
* Window - Preferences - Java - Code Generation - Code and Comments 
*/ 
package com.nari.pmos.sis.client; import java.rmi.RemoteException; 
import java.util.Hashtable; import javax.ejb.CreateException; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; /** 
* @author lxx 

* To change the template for this generated type comment go to 
* Window - Preferences - Java - Code Generation - Code and Comments 
*/ 
public class SessionClient { 
private com.nari.pmos.sis.session.QueryHome getHome() 
throws NamingException { 
return (com.nari.pmos.sis.session.QueryHome) getContext().lookup( 
com.nari.pmos.sis.session.QueryHome.JNDI_NAME); 

private InitialContext getContext() throws NamingException { 
Hashtable props = new Hashtable(); 
props.put(InitialContext.INITIAL_CONTEXT_FACTORY, 
"org.jnp.interfaces.NamingContextFactory"); 
props.put(InitialContext.PROVIDER_URL, "jnp://127.0.0.1:1099"); 
// This establishes the security for authorization/authentication 
// props.put(InitialContext.SECURITY_PRINCIPAL,"username"); 
// props.put(InitialContext.SECURITY_CREDENTIALS,"password"); 
InitialContext initialContext = new InitialContext(props); 
return initialContext; 

public void testBean() { 
try { 
com.nari.pmos.sis.session.Query myBean = getHome().create(); 
//-------------------------------------- 
//This is the place you make your calls. 
//System.out.println(myBean.callYourMethod()); 
System.out.println("Request from client : "); 
System.out.println("Reply from Server: Your userid is " +myBean.loginUser("ANDY","PASSWD")); 
} catch (RemoteException e) { 
e.printStackTrace(); 
} catch (CreateException e) { 
e.printStackTrace(); 
} catch (NamingException e) { 
e.printStackTrace(); 


public static void main(String[] args) { 
SessionClient test = new SessionClient(); 
test.testBean(); 

} Stateless Session Bean QueryBean源码如下: 
/* 
* Created on 2004-10-19 

* To change the template for this generated file go to 
* Window - Preferences - Java - Code Generation - Code and Comments 
*/ 
package com.nari.pmos.sis.session; 
import javax.ejb.SessionBean; 
import javax.ejb.SessionContext; 
import java.util.ArrayList; 
import java.util.Collection; 
import java.util.Iterator; 
import com.nari.pmos.sis.cmp.*; /** 
* @ejb.bean name="Query" 
* jndi-name="QueryBean" 
* type="Stateless" 

* @ejb.dao class="com.nari.pmos.sis.session.QueryDAO" 
* impl-class="com.nari.pmos.sis.dao.QueryDAOImpl" 


* @ejb.resource-ref res-ref-name="jdbc/DefaultDS" 
* res-type="javax.sql.Datasource" 
* res-auth="Container" 

* @jboss.resource-ref res-ref-name="jdbc/DefaultDS" 
* jndi-name="java:/DefaultDS" 


* @ejb.ejb-ref ejb-name="Marketer" 
* view-type="local" 
* ref-name="MarketerLocal" 

* @jboss.ejb-ref-jndi ref-name="Marketer" 
* jndi-name="MarketerLocal" 

**/ 
public abstract class QueryBean implements SessionBean { protected SessionContext ctx; 
private MarketerLocalHome eterLocalHome; /** 
* @ejb.interface-method 
* view-type="remote" 
* @dao.call name="loginUser" 
**/ 
public String loginUser(String username, String password ) { System.out.println("Entering QueryBean"); 
System.out.println("Leaving QueryBean"); 
return null; 
} /** 
* Sets the session context 
* @param javax.ejb.SessionContext the new ctx value 
**/ 
public void setSessionContext(javax.ejb.SessionContext ctx) { 
this.ctx = ctx; 

/** 
* Unsets the session context 
* @param javax.ejb.SessionContext ctx value 
**/ 
public void unsetSessionContext() { 
this.ctx = null; 
} /** 
* @ejb.interface-method 
* view-type="remote" 
**/ public void ejbCreate () throws javax.ejb.CreateException { System.out.println (" Entering QueryBean.ejbCreate() "); 
try { 
eterLocalHome = MarketerUtil.getLocalHome(); } 
catch (Exception e) { 
e.printStackTrace(); 

System.out.println (" Leaving QueryBean.ejbCreate() "); 

/** 
* Returns object MarketerData 
* @ejb.interface-method 
* view-type="remote" 
**/ 
public MarketerData getMarketerData(String keyID){ 
System.out.println("Entering QueryBean.getMarketerData()"); 
MarketerData myMarketer=null; try { 
MarketerLocal eter=eterLocalHome.findByPrimaryKey(keyID); if (eter!=null){ 
myMarketer=eter.getMarketerData(); 

} catch (Exception e){ 
System.out.println("Error in QueryBean.getMarketerData()"+e); 

System.out.println("Leaving Query.getMarketerData()"); 
return myMarketer; 
} /** 
* Returns ArrayList of all eters. 
* @ejb.interface-method 
* view-type="remote" 
**/ 
public java.util.ArrayList getAllMarketers() { System.out.println("Entering QueryBean.getAllMarketers()"); 
Collection eters=null; 
ArrayList etersList=new ArrayList(); try { 
eters=eterLocalHome.findAll(); 
Iterator iterator=eters.iterator(); 
while (iterator.hasNext()) { 
MarketerLocal myMarketerLocal=(MarketerLocal)iterator.next(); 
etersList.add(myMarketerLocal.getMarketerData()); 

}catch (Exception e){ 
System.out.println("Error in QueryBean.getAllMarketers()"+e); 

System.out.println("Leaving QueryBean.getAllMarketers()"); 
return etersList; 
} } 为了访问数据库,还定义了DAO对象,实现DAO接口的类QueryDAOImpl类;源码如下: 
/* 
* Created on 2004-10-19 

* To change the template for this generated file go to 
* Window - Preferences - Java - Code Generation - Code and Comments 
*/ 
package com.nari.pmos.sis.dao; import javax.naming.InitialContext; 
import javax.sql.DataSource; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
/** 
* @author lxx 

* To change the template for this generated type comment go to 
* Window - Preferences - Java - Code Generation - Code and Comments 
*/ 
public class QueryDAOImpl { 
private DataSource jdbcFactory; 
public void init() { 
System.out.println("Entering QueryDAOImpl.init()"); 
InitialContext c=null; if (this.jdbcFactory ==null){ try{ 
c = new InitialContext(); 
this.jdbcFactory= (DataSource)c.lookup("java:comp/env/jdbc/DefaultDS"); }catch (Exception e){ 
System.out.println("Error in QueryDAOImpl.init()"); } 

System.out.println("Leaving QueryDAOImpl.init()"); 

public String loginUser(String username,String password){ 
System.out.println("Entering QueryDAOImpl.loginUser() "); 
Connection conn=null; 
PreparedStatement ps=null; 
ResultSet rs=null; 
String userID=null; 
try { 
conn=jdbcFactory.getConnection(); 
String queryString="select userid from query where username=? and password=?"; 
ps=conn.prepareStatement(queryString); 
ps.setString(1,username); 
ps.setString(2,password); 
rs=ps.executeQuery(); 
boolean result=rs.next(); 
if (result){ 
userID=rs.getString("userid"); 
System.out.println("UserID is "+userID); 

} catch (SQLException e){ 
e.printStackTrace(); 
System.out.println("Inside QueryDAOImpl.loginUser()"+e); 

finally { 
try { 
rs.close(); 
ps.close(); 
conn.close(); 

catch (Exception e){ 


System.out.println("Leaving QueryDAOImpl.loginUser()"); 
return userID; 

} 引起错误的QuerySession是QueryBean按xdoclet.xml文件自动产生的,请大侠解惑,多谢! 

解决方案 »

  1.   

    怎么没有人回答啊,
    我想肯定有人用过这个例子的啊,http://www.tusc.com.au/tutorial/html/index.html
    帮帮忙吧
      

  2.   

    呵呵,所以我希望学过该例子的朋友帮忙啊,主要报错的原因如下: 
    Caused by: java.lang.ClassCastException 
    at com.nari.pmos.sis.session.QuerySession.getDao(QuerySession.java:44) 
    at com.nari.pmos.sis.session.QuerySession.loginUser(QuerySession.java:56) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke
      

  3.   

    ClassCastExceptionThrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. 指出代码尝试把object转型为一个不是该子类实例时抛出该异常
    请大侠帮忙分析下,怎么会有这种情况的发生?
    BTW,谢谢 jyz072020() 
      

  4.   

    ClassCastExceptionThrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. 指出代码尝试把object转型为一个不是该子类实例时抛出该异常
    请大侠帮忙分析下,怎么会有这种情况的发生?
     
     这是问题所在啊,大家来看看啊