您现在的位置: 中国教师站 >> 教师关注 >> 电脑技术 >> 编程技术 >> Java >> 正文

Google
win2k环境下基于JBOSS的J2EE开发实践----之四:BMP实体
作者:Siny 文章来源:中国教师站cn-teacher 点击数: 更新时间:2007-4-22 13:23:21
方法和查找方法,它是一个自解释的类,所有的注解我都放在程序中了。


package account.ejb;
//以下引入所必需的包
import java.sql.*;
import javax.naming.*;
import javax.ejb.*;
import java.util.*;
//BMP实体Bean,它代表银行的一个账户
public class AccountBean implements EntityBean{
//以下是第一部分,定义域和setter/getter方法
protected EntityContext ctx;
//定义BMP管理的状态域,它和数据库中的字段对应的。
private String accountID;//主键
private String ownerName;//账户主姓名
private double balance;//账户上的金额
//构造器
public AccountBean(){
System.out.println("New Bank Account EntityBean Object created by JBOSS Container.");
}
//实体Bean域上的获得器/设置器方法
public double getBalance(){
return this.balance;
}
public void setBalance(double balance){
this.balance = balance;
}
public void setOwnerName(String name){
this.ownerName = name;
}
public String getOwnerName(){
return this.ownerName;
}
public String getAccountID(){
return this.accountID;
}
public void setAccountID(String id){
this.accountID = id;
}
public void setEntityContext(EntityContext ctx){
this.ctx = ctx;
}
public EntityContext getEntityContext(){
return this.ctx;
}
//以下是第二部分,实现由容器调用的方法
//EJB必要的方法,它们由容器调用
//活化时
public void ejbActivate(){
System.out.println("ejbActivate() called!");
}
//钝化时
public void ejbPassivate(){
System.out.println("ejbPassivate() called!");
}
//删除EJB实例时
public void ejbRemove() throws RemoveException{
System.out.println("ejbRemove() called!");
}
//从数据库装入,以保持同步
public void ejbLoad(){
System.out.println("ejbLoad() called!");
//查询实体环境获得当前实体Bean对象的主键,以便我们知道要加载哪个实例
AccountPK pk = (AccountPK)this.ctx.getPrimaryKey();
String id = pk.accountID;
PreparedStatement pstmt = null;
Connection conn = null;
try{
conn = getConnection();//这个是我们自己定义的方法,见后,它从连接池中获取
//通过用账号ID查询,从数据库得到账户记录
pstmt = conn.prepareStatement("select ownerName,balance from accounts where id =?");
pstmt.setString(1,id);
ResultSet rs = pstmt.executeQuery();
rs.next();
//把查询的结果保存到实体Bean对象域中
this.ownerName = rs.getString("ownerName");
this.balance = rs.getDouble("balance");
}catch(Exception ex){
throw new EJBException("Account" + pk +"failed to load from database",ex);
}finally{
try{
if(pstmt!=null)
pstmt.close();
}catch(Exception e){}
try{
if(conn!=null)
conn.close();
}catch(Exception e){}
}
}
//更新数据库,及时反映这个内存中的实体Bean实例的当前值
public void ejbStore(){
System.out.println("ejbStore() called!");
PreparedStatement pstmt = null;
Connection conn = null;
try{
conn = getConnection();
//把当前实体Bean的值放入到数据库中保存,及时更新数据库
pstmt = conn.prepareStatement("update accounts set ownerName =?,balance=? where id=?");
pstmt.setString(1,this.ownerName);
pstmt.setDouble(2,this.balance);
pstmt.setString(3,this.accountID);
pstmt.executeUpdate();
}catch(Exception ex){
throw new EJBException("Account" +accountID + "failed to save to database",ex);
}finally{
try{
if(pstmt!=null)
pstmt.close();
}catch(Exception e){}
try{
if(conn!=null)
conn.close();
}catch(Exception e){}
}
}
//容器调用,把这个Bean实例与一个特定的环境对象分离,固定的方法
public void unsetEntityContext(){
System.out.println("unsetEnetityContext() called!");
this.ctx = null;
}
//在ejbCreate调用之后调用,固定的方法
public void ejbPostCreate(String accountID,String ownerName,double balance){
}
//这是对应于Home接口中的create()方法,返回这个账户的主键
public AccountPK ejbCreate(Str

上一页  [1] [2] [3] [4] [5] [6] 下一页

相关专题:
 
 网友评论:(评论内容只代表网友观点,与本站立场无关!)
GOOGLE广告

阅读排行

| 设为首页 | 加入收藏 | 联系站长 | 友情链接 | 版权申明 |
中国教师站

中国教师站 版权所有 Copyright © 2006-2020 All Rights Reserved 站长:Sina & Siny
[备用域名:www.JXZYW.Com] 有事请留言有事请留言
【实力成就精品 诚信呵护品牌】

信息产业部备案
苏ICP备06018635号