Jbuilder8开发J2ee学习笔记(4)

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

Jbuilder8开发J2ee学习笔记(4)

作者:缪青

1.现在开始写代码:

我们应该知道在BMP中Insert一般写在ejbCreate中,现在我们就填充ejbCreate。

public BMPBEANPK ejbCreate(String shancbz, String xiwbz) throws CreateException {

PreparedStatement pstmt = null;

Connection conn = null;

setShancbz(shancbz);

setXiwbz(xiwbz);

try {

System.out.println("ejbCreate() called.");

this.shancbz = shancbz;

this.xiwbz = xiwbz;

conn = getConnection();

pstmt = conn.prepareStatement(

"insert into Aa11 (shancbz, xiwbz) values (?, ?)");

pstmt.setString(1, shancbz);

pstmt.setString(2, xiwbz);

pstmt.executeUpdate();

return new BMPBEANPK(shancbz);

}

catch (Exception e) {

throw new CreateException(e.toString());

}

finally {

try {

if (pstmt != null) {

pstmt.close();

}

}

catch (Exception e) {}

try {

if (conn != null) {

conn.close();

}

}

catch (Exception e) {}

}

}

为了使用方便我们还建立了一个一模一样的方法ejbHomeEjbInsertAa11。

public BMPBEANPK ejbHomeEjbInsertAa11(String shancbz, String xiwbz) throws CreateException {

/**@todo Complete this method*/

PreparedStatement pstmt = null;

Connection conn = null;

try {

System.out.println("ejbHomeEjbInsertAa11() called.");

this.shancbz = shancbz;

this.xiwbz = xiwbz;

conn = getConnection();

pstmt = conn.prepareStatement(

"insert into Aa11 (shancbz, xiwbz) values (?, ?)");

pstmt.setString(1, shancbz);

pstmt.setString(2, xiwbz);

pstmt.executeUpdate();

return new BMPBEANPK(shancbz);

}

catch (Exception e) {

throw new CreateException(e.toString());

}

finally {

try {

if (pstmt != null) {

pstmt.close();

}

}

catch (Exception e) {}

try {

if (conn != null) {

conn.close();

}

}

catch (Exception e) {}

}

}

这样我们就有了一个很简单的插入方法,以后我们对数据库操作就全靠他了。

为了连接数据库我们还写了getConnection,其中TestDataSource是Weblogic中我们配置过的JNDI。

public Connection getConnection() throws Exception {

try {

ctx = new InitialContext();

javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup(

"TestDataSource");

return ds.getConnection();

}

catch (Exception ex) {

System.err.println("Could not locate datasource! Reason:");

ex.printStackTrace();

throw ex;

}

}

2.配置发布环境:

现在可以开始配置发布环境了,选择RUN->Configurations…,在其中的Server页中选Weblogic Application Server 6.X,然后在RUN页,选择New…,随便新建一个配置,OK全部退出。这是你的Run图标下面会出现你刚才配置好的环境了。

3.发布EJB:

建议你在Jbuilder8外运行Weblogic6.1;

然后Rebuild你的EJB“JSEB”;

现在确保你的Weblogic6.1处于运行状态,右键点击你的JSEB发布

如果你第一次发布,选择Deploy。如果原来发布过,现在想发布,请选择Redeploy。

一切正常,过一会你就会把你的EJB发布到Weblogic6.1上了。

4.开发测试客户端:

File->New->EJB Test Client->Application->

然后Finish。

标准的测试代码生成好了,下面要的是在里面initialize()加入必要代码。

try {

//get naming context

Context context = getInitialContext();

//look up jndi name

Object ref = context.lookup("BMPBEANRemote");

//look up jndi name and cast to Home interface

bMPBEANHome = (BMPBEANHome) PortableRemoteObject.narrow(ref, BMPBEANHome.class);

bMPBEANHome.ejbInsertAa11("1", "J");

//这是通过调用EntityBean的ejbHomeEjbInsertAa11

//功能其是很简单,插入aa11表一条记录("1","J")

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();

}

运行过后看看数据库中表aa11是不是增加了一条记录。

到次BMP的开发结束,下面是SessionBean的内容。

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