1.建立SessionBean
如下图先建立一个SessionBean

由于我们要使用SessionBean去调用BMP中的插入方法,所以这里新建一个方法insTableAa11

很清楚了吧!!
然后开始写insTableAa11的代码。
2.书写代码
public void insTableAa11(String shancbz, String xiwbz) throws CreateException,
      javax.naming.NamingException, java.rmi.RemoteException {
Context TX = new InitialContext();
bMPBEANHome =(BMPBEANHome) javax.rmi.PortableRemoteObject.narrow(TX.
        lookup("BMPBEANRemote"), BMPBEANHome.class);
System.out.println("begin to insert...........");
bMPBEANHome.ejbInsertAa11(shancbz, xiwbz);
  }
 
上面的
Context TX = new InitialContext();
bMPBEANHome =(BMPBEANHome) javax.rmi.PortableRemoteObject.narrow(TX.
        lookup("BMPBEANRemote"), BMPBEANHome.class);
你也可以放到ejbCreate中
 
CreateException, javax.naming.NamingException, java.rmi.RemoteException最好都抛出,应为后面Struts中我们要用到。
 
在SessionBean的EJB Local References中进行如下关联:

3.发布EJB
和上一节讲的一样,使用ReDelpoy发布EJB上Weblogic6.1
4.开发测试程序
和上一节一样,只是对于下面这张图不同
 
  public void initialize() {
    long startTime = 0;
    if (logging) {
      log("Initializing bean access.");
      startTime = System.currentTimeMillis();
    }
    try {
      //get naming context
      Context context = getInitialContext();
      //look up jndi name
      Object ref = context.lookup("SessBean");
      //look up jndi name and cast to Home interface
      sessHome = (SessHome) PortableRemoteObject.narrow(ref, SessHome.class);
      sessHome.create().insTableAa11("2", "S");
      //这是通过Session的insTableAa11调用EntityBean的ejbHomeEjbInsertAa11
      //功能其是很简单,插入Aa11表一条记录("2","S")
      if (logging) {
        long endTime = System.currentTimeMillis();
        log(
        "Succeeded initializing local bean access through Local Home interface.");
        log("Execution time: " + (endTime - startTime) + " ms.");
      }
    }
    catch (Exception e) {
      if (logging) {
        log("Failed initializing bean access.");
      }
      e.printStackTrace();
    }
  }
注意里面的JNDI改变了。现在运行测试程序,是不是又有一条记录插入aa11表。