Jbuilder8开发J2ee学习笔记(5)

王朝java/jsp·作者佚名  2006-01-08
宽屏版  字体: |||超大  

Jbuilder8开发J2ee学习笔记(5)

作者:缪青

开发SessionBean

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表。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有