package Account;
import java.sql.*;
import javax.ejb.*;
import java.util.Collection;
import java.util.Vector;
// 这个管理持久化实体Bean表示一个银行账户
public class AccountBean implements EntityBean
{
protected EntityContext ctx;
// Bean的状态域
private String accountID; // 本例中的主键
private String ownerName;
private double balance;
public AccountBean()
{ }
// 商务逻辑方法
//存款
public void deposit(double amt) throws AccountException {
balance += amt;
}
// 取款,如果amt>余额,抛出异常
public void withdraw(double amt) throws AccountException {
if (amt > balance) {
throw new AccountException(余额不够");
}
balance -= amt;
}
// 实体Bean上的获得器/设置器方法
public double getBalance() {
System.out.println("getBalance()");
return balance;
}
public void setOwnerName(String name) {
System.out.println("setOwnerName()" + accountID);
ownerName = name;
}
public String getOwnerName() {
System.out.println("getOwnerName() on" + accountID);
return ownerName;
}
public String getAccountID() {
System.out.println("getAccountID() on" + accountID);
return accountID;
}
public void setAccountID(String id) {
System.out.println("setAccountID() on " + accountID);
this.accountID = id;
}
// 返回所有账户的余额
public double ejbHomeGetTotalBankValue() throws AccountException {
PreparedStatement pstmt = null;
Connection conn = null;
try {
// 和数据库连接
conn = getConnection();
// 执行查询操作,得到所有账户余额总和
pstmt = conn.prepareStatement(
"select sum(balance) as total from accounts");
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return rs.getDouble("total");
}
}
catch (Exception e) {
e.printStackTrace();
throw new AccountException(e);
}
finally {// 释放数据库资源
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) {}
try { if (conn != null) conn.close(); }
catch (Exception e) {}
}
throw new AccountException("Error!");
}
// EJB必须实现的方法
public void ejbActivate() {
System.out.println("ejbActivate()");
}
// 从数据库中移除实体Bean
public void ejbRemove() throws RemoveException {
System.out.println("ejbRemove()");
// 一个实体Bean可以表示不同的数据实例
// 我们应该通过主键来判断删除哪个数据实例
AccountPK pk = (AccountPK) ctx.getPrimaryKey();
String id = pk.accountID;
PreparedStatement pstmt = null;
Connection conn = null;
try {
// 获得新的数据库连接
conn = getConnection();
// 从数据库中删除账号
pstmt = conn.prepareStatement("delete from accounts where id = ?");
pstmt.setString(1, id);
// 操作数据库失败,抛出一个系统级错误
if (pstmt.executeUpdate() == 0) {
throw new RemoveException("Account " + pk +
" failed to be removed from the database");
}
}
catch (Exception ex) {
throw new EJBException(ex.toString());
}
finally { // 释放数据库连接
try { if (pstmt != null) pstmt.close(); }
catch (Exception e) {}
try { if (conn != null) conn.close(); }
catch (Exception e) {}
}
}
// 钝化,为了挂起释放的资源
public void ejPassivate() {
System.out.println("ejbPassivate ()on " + accountID);
}
// 更新内存中的实体Bean对象,反映数据库中的当前值
public void ejbLoad() {
System.out.println("ejbLoad() on " + accountID);
// 得到实体主键,这样我们知道那个实例被加载
AccountPK pk = (AccountPK) 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();
ownerName = rs.getString("ownerName");
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) {}
}
}
// 更新数据库,反映当前内存中的值
public void ejbStore() {
System.out.println("ejbStore() on " + accountID);
PreparedStatement pstmt = null;
Connection conn = null;
try {
// 获得新的数据库连接
conn = getConnection();
// 往数据库存储账户
pstmt = conn.prepareStatement(
"update accounts set ownerName = ?, balance = ? where id = ?");
pstmt.setString(1, ownerName);
pstmt.setDouble(2, balance);
pstmt.setString(3, 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 setEntityContext(EntityContext ctx) {
System.out.println("setEntityContext called");
this.ctx = ctx;
}
// 把一个Bean和它的环境变量分离
public void unsetEntityContext() {
System.out.println("unsetEntityContext called on " + accountID);
this.ctx = null;
}
// 在ejbCreate()调用后,Bean可以从它的环境中提取EJB对象
// 并当作一个this参数传递
public void ejbPostCreate(String accountID, String ownerName) {
}
// 此方法对应Home中的create()方法
// 当客户端调用本地对象(Home)的create()方法时,
// 本地对象调用ejbCreate()方法
public AccountPK ejbCreate(String accountID, String ownerName)
throws CreateException {
PreparedStatement pstmt = null;
Connection conn = null;
try {
System.out.println("ejbCreate() called on " + accountID);
this.accountID = accountID;
this.ownerName = ownerName;
this.balance = 0;
// 获得数据库的连接
conn = getConnection();
// 插入账号
pstmt = conn.prepareStatement(
"insert into accounts (id, ownerName, balance) values (?, ?, ?)");
pstmt.setString(1, accountID);
pstmt.setString(2, ownerName);
pstmt.setDouble(3, balance);
pstmt.executeUpdate();
// 产生一个主键,并返回
return new AccountPK(accountID);
}
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) {}
}
}
// 通过主键找到一个账号
public AccountPK ejbFindByPrimaryKey(AccountPK key)
throws FinderException {
PreparedStatement pstmt = null;
Connection conn = null;
try {
Sys
没有合适的资源?快使用搜索试试~ 我知道了~
基于java的开发源码-EJB 模拟银行ATM流程及操作源代码.zip
共7个文件
java:6个
xml:1个
1 下载量 60 浏览量
2024-01-07
18:37:23
上传
评论
收藏 7KB ZIP 举报
温馨提示
基于 基于java的开发源码-EJB 模拟银行ATM流程及操作源代码.zipjava的开发源码-EJB 模拟银行ATM流程及操作源代码.zip 基于java的开发源码-EJB 模拟银行ATM流程及操作源代码.zip 基于java的开发源码-EJB 模拟银行ATM流程及操作源代码.zip 基于java的开发源码-EJB 模拟银行ATM流程及操作源代码.zip 基于java的开发源码-EJB 模拟银行ATM流程及操作源代码.zip 基于java的开发源码-EJB 模拟银行ATM流程及操作源代码.zip 基于java的开发源码-EJB 模拟银行ATM流程及操作源代码.zip 基于java的开发源码-EJB 模拟银行ATM流程及操作源代码.zip 基于java的开发源码-EJB 模拟银行ATM流程及操作源代码.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于java的开发源码-EJB 模拟银行ATM流程及操作源代码.zip (7个子文件)
codefans.net
BMP
AccountBean.java 10KB
AccountPK.java 546B
ejb-jar.xml 992B
AccountException.java 278B
AccountHome.java 764B
Account.java 746B
AccountClient.java 3KB
共 7 条
- 1
资源评论
快乐无限出发
- 粉丝: 1127
- 资源: 7260
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Surfer,线性函数
- MyBatis 的动态 SQL 是其核心特性之一.txt
- 时代的sdddsddsddsd
- 基于哈希链表的简单人员信息管理系统
- 其他类别JdonFramework开源框架 v5.1 Build20071025-jdonframework-5.1.rar
- 2001~2022年上市公司数字赋能指数.dta
- 2001~2022年上市公司数字赋能指数.xlsx
- 信息办公石大在线财务管理系统(含源码)-shidacaiwu.rar
- 信息办公电信计费系统完整代码-netctossconformity.rar
- matlab实现TD-SCDMA中初始同步捕捉DwPTS下行同步导频时隙的仿真.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功