?? bmptradebean.java
字號(hào):
package bmp;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Vector;
import javax.ejb.CreateException;
import javax.ejb.DuplicateKeyException;
import javax.ejb.EJBException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.FinderException;
import javax.ejb.NoSuchEntityException;
import javax.ejb.ObjectNotFoundException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
/**
* <p>Title: 實(shí)現(xiàn)類(lèi)</p>
* <p>Description: EJBean管理的JDBC持續(xù)性管理和事務(wù)管理;在這個(gè)文件中的代碼直接訪問(wèn)數(shù)據(jù)庫(kù);</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Filename: bmpTradeBean.java</p>
* @author 杜江
* @version 1.0
*/
public class bmpTradeBean implements EntityBean {
//設(shè)置是否打印控制臺(tái)
final static private boolean VERBOSE = true;
//聲明實(shí)體上下文變量
private EntityContext ctx;
private String FundId;
private double baseFunds;
/**
* 為EJBean設(shè)置實(shí)體EJB上下文
* @參數(shù) ctx EntityContext
*/
public void setEntityContext(EntityContext ctx) {
log("setEntityContext called");
this.ctx = ctx;
}
//取消實(shí)體上下文設(shè)置
public void unsetEntityContext() {
log("unsetEntityContext (" + id() + ")");
this.ctx = null;
}
//這是本類(lèi)必須實(shí)現(xiàn)的方法,在本例中沒(méi)有用到
public void ejbActivate() {
log("ejbActivate (" + id() + ")");
}
//這是本類(lèi)必須實(shí)現(xiàn)的方法,在本例中沒(méi)有用到
public void ejbPassivate() {
log("ejbPassivate (" + id() + ")");
}
/**
* 從數(shù)據(jù)庫(kù)中加載EJB
* @異常 javax.ejb.NoSuchEntityException 如果在數(shù)據(jù)庫(kù)中沒(méi)有找到Bean
* @異常 javax.ejb.EJBException 通訊或系統(tǒng)錯(cuò)誤
*/
public void ejbLoad() {
log("ejbLoad: (" + id() + ")");
//聲明數(shù)據(jù)庫(kù)連接對(duì)象
Connection con = null;
//聲明SQL命令預(yù)處理對(duì)象
PreparedStatement ps = null;
//找到賬號(hào)主鍵
FundId = (String) ctx.getPrimaryKey();
try {
//獲取數(shù)據(jù)庫(kù)連接
con = getConnection();
//設(shè)置SQL命令,讀取記錄
ps = con.prepareStatement("select fund from tbl_Funds where id = ?");
ps.setString(1, FundId);
//執(zhí)行SQL
ps.executeQuery();
//獲取SQL結(jié)果
ResultSet rs = ps.getResultSet();
if (rs.next()) {
//取得數(shù)據(jù)
baseFunds = rs.getDouble(1);
} else {
String error = "ejbLoad: beansTeadeBean (" + FundId + ") not found !";
log(error);
throw new NoSuchEntityException (error);
}
} catch (SQLException sqe) {
//數(shù)據(jù)庫(kù)異常處理
log("SQLException: " + sqe);
throw new EJBException(sqe);
} finally {
cleanup(con, ps);
}
}
/**
* 數(shù)據(jù)庫(kù)中存入EJBean
* @異常 javax.ejb.NoSuchEntityException 如果在數(shù)據(jù)庫(kù)中沒(méi)有找到Bean
* @異常 javax.ejb.EJBException 通訊或系統(tǒng)錯(cuò)誤
*/
public void ejbStore() {
log("ejbStore (" + id() + ")");
//聲明數(shù)據(jù)庫(kù)連接對(duì)象
Connection con = null;
//聲明SQL命令預(yù)處理對(duì)象
PreparedStatement ps = null;
try {
//獲取數(shù)據(jù)庫(kù)連接
con = getConnection();
//設(shè)置SQL命令,更新數(shù)據(jù)庫(kù)
ps = con.prepareStatement("update tbl_Funds set fund = ? where id = ?");
ps.setDouble(1, baseFunds);
ps.setString(2, FundId);
//執(zhí)行SQL
if (!(ps.executeUpdate() > 0)) {
String error = "ejbStore: bmpTradeBean (" + FundId + ") not updated !";
log(error);
throw new NoSuchEntityException (error);
}
} catch(SQLException sqe) {
//數(shù)據(jù)庫(kù)次操作異常處理
log("SQLException: " + sqe);
throw new EJBException (sqe);
} finally {
cleanup(con, ps);
}
}
/**
* 這個(gè)方法和"TblUserInfoBean.java"中定義的的Bean的"ejbCreate"方法相對(duì)應(yīng)
* 這兩個(gè)方法的參數(shù)應(yīng)該相同。當(dāng)客戶(hù)端調(diào)用"TblUserInfoHome.create()"方法時(shí),EJB容器
* 會(huì)找到EJBean的實(shí)例,并調(diào)用它的"ejbCreate()"方法。
* 對(duì)容器管理的ejb,ejbCreate方法返回為null,而bean管理的ejb,返回的是主鍵類(lèi)。
* @參數(shù) FundId String 賬號(hào)ID
* @參數(shù) initialbaseFunds double 初始化結(jié)算值
* @異常 javax.ejb.CreateException 創(chuàng)建bean錯(cuò)誤時(shí)拋出的異常
*/
public String ejbCreate(String FundId, double initialbaseFunds)
throws CreateException
{
//日志信息
log("bmpTradeBean.ejbCreate( id = " +FundId+ ", " + "initial baseFunds = $ " + initialbaseFunds + ")");
this.FundId = FundId;
this.baseFunds = initialbaseFunds;
//聲明數(shù)據(jù)庫(kù)連接
Connection con = null;
PreparedStatement ps = null;
try {
//獲取數(shù)據(jù)庫(kù)連接
con = getConnection();
//執(zhí)行sql語(yǔ)句,插入記錄
ps = con.prepareStatement("insert into tbl_Funds (id, fund) values (?, ?)");
ps.setString(1, FundId);
ps.setDouble(2, baseFunds);
if (ps.executeUpdate() != 1) {
String error = "JDBC did not create any row";
log(error);
throw new CreateException (error);
}
log("JDBC create one row!");
return FundId;
} catch (SQLException sqe) {
///異常處理
try {
//查找主鍵
ejbFindByPrimaryKey(FundId);
} catch(ObjectNotFoundException onfe) {
String error = "SQLException: " + sqe;
log(error);
throw new CreateException (error);
}
String error = "An Account already exists in the database with Primary Key " + FundId;
log(error);
throw new DuplicateKeyException(error);
} finally {
cleanup(con, ps);
}
}
//這是本類(lèi)必須實(shí)現(xiàn)的方法,在本例中沒(méi)有用到
public void ejbPostCreate(String FundId, double initialbaseFunds) {
log("ejbPostCreate (" + id() + ")");
}
/**
* 從數(shù)據(jù)庫(kù)中刪除EJBean
* @異常 javax.ejb.NoSuchEntityException 如果數(shù)據(jù)庫(kù)中沒(méi)找到這個(gè)ejb
* @異常 javax.ejb.EJBException 通信錯(cuò)誤拋出的異常
*/
public void ejbRemove() {
log("ejbRemove (" + id() + ")");
//聲明數(shù)據(jù)庫(kù)連接
Connection con = null;
PreparedStatement ps = null;
try {
//獲取連接
con = getConnection();
//獲取主鍵
FundId = (String) ctx.getPrimaryKey();
//執(zhí)行SQL語(yǔ)句,刪除記錄
ps = con.prepareStatement("delete from tbl_Funds where id = ?");
ps.setString(1, FundId);
if (!(ps.executeUpdate() > 0)) {
String error = "bmpTradeBean (" + FundId + " not found";
log(error);
throw new NoSuchEntityException (error);
}
} catch (SQLException sqe) {
//異常處理
log("SQLException: " + sqe);
throw new EJBException (sqe);
} finally {
//清除
cleanup(con, ps);
}
}
/**
* 給定主鍵查找EJBean
* @參數(shù) pk String 主鍵
* @異常 javax.ejb.ObjectNotFoundException EJBean沒(méi)發(fā)現(xiàn)拋出的異常
* @異常 javax.ejb.EJBException 系統(tǒng)出現(xiàn)通訊故障時(shí)拋出
*/
public String ejbFindByPrimaryKey(String pk)
throws ObjectNotFoundException
{
log("ejbFindByPrimaryKey (" + pk + ")");
//聲明數(shù)據(jù)庫(kù)連接
Connection con = null;
PreparedStatement ps = null;
try {
//獲取連接
con = getConnection();
//查詢(xún)主鍵對(duì)應(yīng)的記錄
ps = con.prepareStatement("select fund from tbl_Funds where id = ?");
ps.setInt(1, Integer.parseInt(pk));
ps.executeQuery();
//獲取結(jié)果集
ResultSet rs = ps.getResultSet();
if (rs.next()) {
baseFunds = rs.getDouble(1);
} else {
//沒(méi)有發(fā)現(xiàn)這個(gè)主鍵值的ejb
String error = "ejbFindByPrimaryKey: beansTeadeBean (" + pk + ") not found";
log(error);
throw new ObjectNotFoundException (error);
}
} catch (SQLException sqe) {
//異常處理
log("SQLException: " + sqe);
throw new EJBException (sqe);
} finally {
//清除
cleanup(con, ps);
}
log("ejbFindByPrimaryKey (" + pk + ") found");
return pk;
}
/**
* 查找所有結(jié)算大于給定值的EJBeans
* @參數(shù) baseFundsGreaterThan double 賬戶(hù)資金
* @返回 Collection
* @異常 javax.ejb.EJBException 通信錯(cuò)誤拋出的異常
*/
public Collection ejbFindBigAccounts(double baseFundsGreaterThan) {
log("ejbFindBigAccounts (baseFunds > " + baseFundsGreaterThan + ")");
//聲明數(shù)據(jù)庫(kù)連接
Connection con = null;
PreparedStatement ps = null;
try {
//獲取連接
con = getConnection();
ps = con.prepareStatement("select id from tbl_Funds where fund > ?");
ps.setDouble(1, baseFundsGreaterThan);
ps.executeQuery();
//獲取結(jié)果集
ResultSet rs = ps.getResultSet();
Vector v = new Vector();
String pk;
while (rs.next()) {
pk = rs.getString(1);
v.addElement(pk);
}
//返回集合
return v;
} catch (SQLException sqe) {
//異常處理
log("SQLException: " + sqe);
throw new EJBException (sqe);
} finally {
//清除
cleanup(con, ps);
}
}
/*
*方法說(shuō)明:添加資金
* @參數(shù):baseFunds 資金數(shù)
* @返回:
* @異常:Exception 當(dāng)增加資金為負(fù)數(shù)時(shí)
*/
public void addFunds(double baseFunds) throws Exception {
if (baseFunds<0)
throw new Exception("Invalid baseFunds");
this.baseFunds+=baseFunds;
}
/*
*方法說(shuō)明:提取資金
* @參數(shù):baseFunds 資金數(shù)
* @返回:
* @異常:Exception 當(dāng)增加資金為負(fù)數(shù)和所提取資金超過(guò)賬戶(hù)上資金時(shí)
*/
public void removeFunds(double baseFunds) throws Exception {
if(baseFunds<0)
throw new Exception("Invalid baseFunds");
if(this.baseFunds<baseFunds)
throw new Exception("the baseFunds less than baseFunds");
this.baseFunds-=baseFunds;
}
/*
*方法說(shuō)明:查詢(xún)賬戶(hù)資金數(shù)
* @返回:double 資金數(shù)
*/
public double getBalance() {
return this.baseFunds;
}
/**
* 從連接池中獲取當(dāng)前連接
* @返回 連接
* @異常 javax.ejb.EJBException 通信錯(cuò)誤
*/
private Connection getConnection()
throws SQLException
{
//聲明初始化上下文
InitialContext initCtx = null;
try {
initCtx = new InitialContext();
//查找數(shù)據(jù)源
DataSource ds = (javax.sql.DataSource)
initCtx.lookup("java:comp/env/jdbc/myDB");
//返回?cái)?shù)據(jù)源連接
return ds.getConnection();
} catch(NamingException ne) {
//有異常
log("UNABLE to get a connection from myDB!");
log("Please make sure that you have setup the connection pool properly");
throw new EJBException(ne);
} finally {
try {
if(initCtx != null) initCtx.close();
} catch(NamingException ne) {
log("Error closing context: " + ne);
throw new EJBException(ne);
}
}
}
// 也可以使用WebLogic的日志服務(wù)
private void log(String s) {
if (VERBOSE) System.out.println(s);
}
// 返回這個(gè)beans的id
private String id() {
return "PK = " + (String) ctx.getPrimaryKey();
}
//清除連接
private void cleanup(Connection con, PreparedStatement ps) {
try {
if (ps != null) ps.close();
} catch (Exception e) {
log("Error closing PreparedStatement: "+e);
throw new EJBException (e);
}
try {
if (con != null) con.close();
} catch (Exception e) {
log("Error closing Connection: " + e);
throw new EJBException (e);
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -