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

Google
win2k环境下基于JBOSS的J2EE开发实践----之四:BMP实体
作者:Siny 文章来源:中国教师站cn-teacher 点击数: 更新时间:2007-4-22 13:23:21
ing accountID,String ownerName,double balance) throws CreateException{
PreparedStatement pstmt = null;
Connection conn = null;
try{ //在这里通入客户端调用时传入的参数创建一个实体Bean的实例
System.out.println("ejbCreate() called!");
this.accountID = accountID;
this.ownerName = ownerName;
this.balance = balance;
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 void storeinto(double amt) throws AccountException{
System.out.println("storeinto("+amt+")called!");
this.balance += amt;
}
//从银行账户中取出一定的钱
public void getout(double amt) throws AccountException{
System.out.println("getout("+amt+")called.");
//如果要取的钱大于账户余额
if(amt>balance){
throw new AccountException("Your balance is not enough!");
}
this.balance -= amt;
}
//从连接池获得数据库的JDBC连接的方法
public Connection getConnection() throws Exception{
try{
Context ctx = new InitialContext();
javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("java:/MySql");//这里要注意一致
return ds.getConnection();
}catch(Exception e){
System.out.println("Couldn't get dataSource!");
e.printStackTrace();
throw e;
}
}
//第四部分,定义并实现Home接口中定义的查找方法,要注意它们的命名和Home接//口中的不同之处
//用主键查找一个账户
//注意,它的命名方式是前面加一个ejb然后把Home接口中对应的方法名的find改为//第一个字母大写
//每个实体EJB必须定义一个此方法
public AccountPK ejbFindByPrimaryKey(AccountPK key) throws FinderException{
PreparedStatement pstmt = null;
Connection conn = null;
try{
System.out.println("ejbFindByPrimaryKey() called!");
conn = getConnection();
pstmt = conn.prepareStatement("select id from accounts where id =?");
pstmt.setString(1,key.toString());
ResultSet rs = pstmt.executeQuery();
rs.next();
//没有错误发生,说明找到了,返回主键
return key;
}catch(Exception e){
throw new FinderException(e.toString());
}finally{
try{
if(pstmt!=null)
pstmt.close();
}catch(Exception e){}
try{
if(conn!=null)
conn.close();
}catch(Exception e){}
}
}
//用它的姓名查找一个账户
//注意,它的命名方式是前面加一个ejb然后把Home接口中对应的方法名的find改为//第一个字母大写
public Collection ejbFindByOwnerName(String name) throws FinderException{
PreparedStatement pstmt = null;
Connection conn = null;
Vector v =new Vector();
try{
System.out.println("ejbFindByOwnerName("+name+") called!");
conn = getConnection();
pstmt = conn.prepareStatement("select id from accounts where ownerName =?");
pstmt.setString(1,name);
ResultSet rs = pstmt.executeQuery();
while (rs.next()){
String id = rs.getString("id");
v.addElement(new AccountPK(id));
}
return v;
}catch(Exception e){
throw new FinderException(e.toString());
}finally{
try{
if(pstmt!=null)
pstmt.close();
}catch(Exception e){}
try{
if(conn!=null)
conn.close();
}catch(Exception e){}
}
}
//它返回银行中所有银行账号的余额总和
//这个方法独立于所有的账户(即EJB实例),可以认为它是一个所有EJB实例共有的方法
//注意:它的命名方式是在上面的情况下还多加了一个Home
public double ejbHomeGetTotalBankValue() throws AccountException{
PreparedStatement pstmt = null;

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

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

阅读排行

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

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

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