Jbuilder8开发J2ee学习笔记(4) (转)

Jbuilder8开发J2ee学习笔记(4) (转)[@more@]

Jbuilder8开发J2EE学习笔记(4)XML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

    作者:缪青

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,其中TestDatasourcewebLOGIC中我们配置过的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发布

ASPectratio="t">

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

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

4.开发测试客户端:

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

 .NET/develop/article/images/clip_image002_19267.png" align=baseline border=0>

 

然后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的内容。


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-997885/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-997885/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值