📄 bmpaccountbean.java
字号:
/*
* Created on 2004-7-1
*
* To change the template for this generated file go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
package com.ejbstudy;
import java.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
/**
* @ejb.bean name="BMPAccount"
* jndi-name="BMPAccountBean"
* type="BMP"
*
*--
* This is needed for JOnAS.
* If you are not using JOnAS you can safely remove the tags below.
* @jonas.bean ejb-name="BMPAccount"
* jndi-name="BMPAccountBean"
*
*--
**/
public abstract class BMPAccountBean implements EntityBean {
private String id; // PK
private String ownerName;
private double balance;
private Address address=new Address();
private EntityContext ctx;
private DataSource ds;
private String dbjndi="java:/MySqlDS";
private Connection con;
public BMPAccountBean() {
System.out.println("New Bank Account Entity Bean Java Object created by EJB Container.");
}
/**
* @return Returns the id.
*/
public String getid() {
return id;
}
/**
* @param id The id to set.
*/
public void setid(String id) {
this.id = id;
}
/**
* @return Returns the address.
*/
public Address getAddress() {
return address;
}
/**
* @param address The address to set.
*/
public void setAddress(Address address) {
this.address = address;
}
/**
* @return Returns the balance.
*/
public double getBalance() {
return balance;
}
/**
* @param balance The balance to set.
*/
public void setBalance(double balance) {
this.balance = balance;
}
/**
* @return Returns the ownerName.
*/
public String getOwnerName() {
return ownerName;
}
/**
* @param ownerName The ownerName to set.
*/
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
/** * @ejb.create-method * view-type="remote" **/public String ejbCreate(String id, String ownerName, double balance,Address address) throws CreateException{ if(id==null)
throw new CreateException("The id is required");
try{
String sql="INSERT INTO account VALUES(?,?,?,?,?,?)";
con=ds.getConnection();
PreparedStatement stmt =con.prepareStatement(sql);
stmt.setString(1,id);
stmt.setString(2,ownerName);
stmt.setDouble(3,balance);
stmt.setString(4,address.getCity());
stmt.setString(5,address.getStreet());
stmt.setInt(6,address.getHouseno());
stmt.executeUpdate();
stmt.close();
}catch (SQLException se){
throw new EJBException(se);
}finally{
try{
if(con!=null)
con.close();
}catch(SQLException se){}
}
this.id=id;
this.ownerName=ownerName;
this.balance=balance;
this.address=address;
//由Bean负责事务持久性,Bean负责返回主键值
return id;}
public void ejbPostCreate(String id, String ownerName,double balance,Address address){
}
//根据id值提取数据
public void ejbLoad(){
try{
String sql="SELECT id,ownername,balance,city,street,houseno FROM account WHERE id=?";
con=ds.getConnection();
PreparedStatement stmt =con.prepareStatement(sql);
stmt.setString(1,this.id);
ResultSet rset=stmt.executeQuery();
if(rset.next()){
this.ownerName=rset.getString("ownerName");
this.balance=rset.getDouble("balance");
this.address.setCity(rset.getString("city"));
this.address.setStreet(rset.getString("street"));
this.address.setHouseno(rset.getInt("houseno"));
stmt.close();
}else{
stmt.close();
throw new NoSuchEntityException("BOOK ID:"+this.id);
}
}catch (SQLException se){
throw new EJBException(se);
}finally{
try{
if(con!=null)
con.close();
}catch(SQLException se){}
}
}
//保存被关联的数据记录
public void ejbStore(){
try{
String sql="UPDATE account SET ownername=?,balance=? ,city =?,street=?,houseno=? WHERE id=?";
con=ds.getConnection();
PreparedStatement stmt =con.prepareStatement(sql);
stmt.setString(1,ownerName);
stmt.setDouble(2,balance);
stmt.setString(3,address.getCity());
stmt.setString(4,address.getStreet());
stmt.setInt(5,address.getHouseno());
if(stmt.executeUpdate()!=1){
stmt.close();
throw new EJBException("occount a error on saved");
}
stmt.close();
}catch (SQLException se){
throw new EJBException(se);
}finally{
try{
if(con!=null)
con.close();
}catch(SQLException se){}
}
}
//删除关联的记录
public void ejbRemove()throws RemoveException{
try{
String sql="DELETE FROM account WHERE id=?";
con=ds.getConnection();
PreparedStatement stmt =con.prepareStatement(sql);
stmt.setString(1,this.id);
if(stmt.executeUpdate()!=1)
throw new RemoveException(("occount a error on remove"));
stmt.close();
}catch (SQLException se){
throw new EJBException(se);
}finally{
try{
if(con!=null)
con.close();
}catch(SQLException se){}
}
}
public void unsetEntityContext(){
this.ctx=null;
}
//初始化数据库连接,初始化情境参数
public void setEntityContext(EntityContext context){
this.ctx=context;
try{
InitialContext initial =new InitialContext();
ds=(DataSource)initial.lookup(this.dbjndi);
}catch(NamingException ne){
throw new EJBException(ne);
}
}
//在Bean激活时,从情境参数中获取Bean的主键值,然后会自动调用ejbLoad()
public void ejbActivate(){
this.id=(String)ctx.getPrimaryKey();
}
//解除当前Bean实例与数据库记录的关系
public void ejbPassivate(){
this.id=null;
}
//根据主键查找对象
public String ejbFindByPrimaryKey(String primarykey)
throws FinderException{
try{
String sql="SELECT id FROM account WHERE id=?";
con=ds.getConnection();
PreparedStatement stmt =con.prepareStatement(sql);
stmt.setString(1,primarykey);
ResultSet rset=stmt.executeQuery();
if(!rset.next()){
stmt.close();
throw new ObjectNotFoundException();
}
stmt.close();
//查到数据库中存在此条记录
return primarykey;
}catch (SQLException se){
throw new EJBException(se);
}finally{
try{
if(con!=null)
con.close();
}catch(SQLException se){}
}
}
//查找帐户余额在指定范围内的Bean的集合
public Collection ejbFindInBalance(double lowerLimit,double upperLimit)
throws FinderException{
try{
String sql="SELECT id FROM account WHERE balance BETWEEN ? AND ?";
System.out.println(sql);
con=ds.getConnection();
PreparedStatement stmt =con.prepareStatement(sql);
stmt.setDouble(1,lowerLimit);
stmt.setDouble(2,upperLimit);
ResultSet rset=stmt.executeQuery();
ArrayList booklist=new ArrayList();
while(rset.next())
booklist.add(rset.getString("id"));
stmt.close();
return booklist;
}catch (SQLException se){
throw new EJBException(se);
}finally{
try{
if(con!=null)
con.close();
}catch(SQLException se){}
}
}/** * @ejb.interface-method * view-type="remote" **/public void deposit(double amt) throws Exception{
balance+=amt;}/** * @ejb.interface-method * view-type="remote" **/public void withdraw(double amt) throws Exception{
if(balance-amt<10)throw new Exception("not enough money");
else balance-=amt;} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -