动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   Jsp → 阅读文章

 完善Bean实体

作者来源: 
阅读 数 472 人次 , 2006-4-19 1:35:00 


  接着我们就必须来完成Bean实体自己了。这个实体所做的工作与session bean中的工做大致相同,但是它的继承的父类不是session bean了而是EntiyBean了。具体代码如下:

package net.chinacode.addressbook;

import javax.ejb.*;
import java.rmi.*;

public class AddressEntryBean extends Object implements EntityBean {

  public static int instanceCount = 0;

  private transient TraceHelper tracer;

  public AddressEntryBean() {
   int instanceNr = instanceCount++;
   tracer = new TraceHelper("AddressEntryBean[" + instanceCount + ']');
   tracer.trace("");
  }

  public String name;
  public String address;
  public String city;

  public String getName() {
   tracer.trace("getName", name);
   return name;
  }

  public String getAddress() {
   tracer.trace("getAddress", address);
   return address;
  }

  public String getCity() {
   tracer.trace("getCity", city);
   return city;
  }

  public void setAddress(String newAddress) {
   tracer.trace("setAddress", new String[] { newAddress });
   address = newAddress;
  }

  public void setCity(String newCity) {
   tracer.trace("setCity", new String[] { newCity });
   city = newCity;
  }

  public void ejbActivate() {
   tracer.trace("ejbActivate");
  }

  public void ejbStore() {
   tracer.trace("ejbStore");
  }

  public void setEntityContext(EntityContext entityContext) {
   tracer.trace("setEntityContext",
   new String[] { String.valueOf(entityContext) });
  }

  public void unsetEntityContext() {
   tracer.trace("unsetEntityContext");
  }

  public void ejbPassivate() {
   tracer.trace("ejbPassivate");
  }

  public void ejbLoad() {
   tracer.trace("ejbLoad");
  }

  public void ejbRemove() {
  tracer.trace("ejbRemove");
  }

  public String ejbCreate(String initialName,
               String initialAddress,
               String initialCity)
  throws CreateException, RemoteException {
   tracer.trace("ejbCreate", new String[] { initialName,
                             initialAddress,
                             initialCity }, initialName);
   name = initialName;
   address = initialAddress;
   city = initialCity;
   return initialName;
  }

  public void ejbPostCreate(String initialName,
                String initialAddress,
                String initialCity)
  throws CreateException, RemoteException {
   tracer.trace("ejbPostCreate", new String[] { initialName,
                           initialAddress,
                           initialCity} );
  }
}

  我们将这段代码存入hdsite\src\java\net\chinacode\addressbook\AddressEntryBean.java文件中。这里使用了一个tracer的TraceHelper类,它只是用来向orion终端输入调试信息的。除去了tracer的代码,哪么剩余的代码已经变的很少了,而且好像没有任何操作的地方。这就是EJB帮助我们完成了所有的事。
  以下列出TraceHelper类的trace方法的代码,由于篇幅所限这里不列出全的代码了:

private void trace(String methodName,
              String[] arguments,
              String returnValue,
              boolean withReturnValue) {
    final DateFormat formatter = new SimpleDateFormat("hh:mm:ss");
    Date now = new Date();
    stream.print(formatter.format(now));
    stream.print(' ');
    stream.print(name);
    stream.print(": ");
    stream.print(methodName);
    stream.print('(');

    int count = (arguments == null) ? 0 : arguments.length;
    if (count > 0) {
      if (arguments[0] == null) {
        stream.print("null");
      } else {
        stream.print('"');
        stream.print(arguments[0]);
        stream.print('"');
      }
    }
    for (int i=1; i       stream.print(", ");
      if (arguments[i] == null) {
        stream.print("null");
      } else {
          stream.print('"');
          stream.print(arguments[i]);
          stream.print('"');
      }
    }
    stream.print(')');

    if (withReturnValue) {
      stream.print(" -> ");

      if (returnValue == null) {
        stream.print("null");
      } else {
        stream.print('"');
        stream.print(returnValue);
        stream.print('"');
      }
    }

    stream.println();
   }

  这些完成后我们就完成了一个完整的实体Bean。具体与数据库交互时需要向数据库提交的信息我们必须写在配置文件中,如下:

 
   address book entry bean
  Address book entry
  net.chinacode.addressbook.AddressEntry
  net.chinacode.addressbook.AddressBook
  net.chinacode.addressbook.AddressEntry
  net.chinacode.addressbook.AddressEntryBean
  Container
  name
  java.lang.String
  False
  name
  address
  city
 



  我们将这段xml加入到之前我们写好的ejb-jar.xml文件的enterprise-bean段中去。我们可以看到在这里我们不但如之前描述session bean一样的说明了EJB的结构,而且还说明了这个实体Bean的存在数据库中的字段,以当这些字段中哪些字段为主键。Orion会依照这个说明自动的去创建数据库同时去数据库中进行查询。   
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:用JSP文件上载轻松实现的方法
· 下一篇:安装配置篇(三)--WebSphere
· 也谈JSP与XML的交互
· JSP编程进度条设计实例(3)
· JBuilder2005实战JSP之创建数据库表
· JSP学习笔记(五)-----JSP中使用JavaBean
· Taglib 原理和实现之支持El表达式


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077