?? accountbean.java
字號:
package com.wiley.compBooks.roman.entity.account;
import java.sql.*;
import javax.naming.*;
import javax.ejb.*;
import java.util.*;
import java.rmi.RemoteException;
/**
* Demonstration Bean-Managed Persistent Entity Bean.
* This Entity Bean represents a Bank Account.
*/
public class AccountBean implements EntityBean {
protected EntityContext ctx;
// Bean-managed state fields
public String accountID; // PK
public String ownerName;
public double balance;
// Environment properties the bean was deployed with
public Properties env;
public AccountBean() {
System.out.println("New Bank Account Entity Bean Java Object created by EJB Container.");
}
//
// Business Logic Methods
//
/**
* Deposits amt into account.
*/
public void deposit(double amt) throws AccountException {
System.out.println("deposit(" + amt + ") called.");
balance += amt;
}
/**
* Withdraws amt from bank account.
* @throw AccountException thrown in amt < available balance
*/
public void withdraw(double amt) throws AccountException {
System.out.println("withdraw(" + amt + ") called.");
if (amt > balance) {
throw new AccountException("Your balance is " + balance + "! You cannot withdraw " + amt + "!");
}
balance -= amt;
}
// Getter/setter methods on Entity Bean fields
public double getBalance() {
System.out.println("getBalance() called.");
return balance;
}
public void setOwnerName(String name) {
System.out.println("setOwnerName() called.");
ownerName = name;
}
public String getOwnerName() {
System.out.println("getOwnerName() called.");
return ownerName;
}
public String getAccountID() {
System.out.println("getAccountID() called.");
return accountID;
}
public void setAccountID(String id) {
System.out.println("setAccountID() called.");
this.accountID = id;
}
//
// EJB-required methods
//
/**
* Called by Container. Implementation can acquire
* needed resources.
*/
public void ejbActivate() throws RemoteException {
System.out.println("ejbActivate() called.");
}
/**
* Removes entity bean data from the database.
* Corresponds to when client calls home.remove().
*/
public void ejbRemove() throws RemoteException {
System.out.println("ejbRemove() called.");
/*
* Remember that an entity bean class can be used to
* represent different data instances. So how does
* this method know which instance in the database
* to delete?
*
* The answer is to query the container by calling
* the entity context object. By retrieving the
* primary key from the entity context, we know
* which data instance, keyed by the PK, that we
* should delete from the DB.
*/
AccountPK pk = (AccountPK) ctx.getPrimaryKey();
String id = pk.accountID;
PreparedStatement pstmt = null;
Connection conn = null;
try {
/*
* 1) Acquire a new JDBC Connection
*/
conn = getConnection();
/*
* 2) Remove account from the DB
*/
pstmt = conn.prepareStatement("delete from accounts where id = ?");
pstmt.setString(1, id);
/*
* 3) Throw a system-level exception if something
* bad happened.
*/
if (pstmt.executeUpdate() == 0) {
throw new RemoteException("Account " + pk + " failed to be removed from the database");
}
}
catch (SQLException ex) {
throw new RemoteException(ex.toString());
}
finally {
/*
* 4) Release the DB Connection
*/
try {
if (pstmt != null) pstmt.close();
}
catch (Exception e) { }
try {
if (conn != null) conn.close();
}
catch (Exception e) { }
}
}
/**
* Called by Container. Releases held resources for
* passivation.
*/
public void ejbPassivate() throws RemoteException {
System.out.println("ejbPassivate () called.");
}
/**
* Called by the container. Updates the in-memory entity
* bean object to reflect the current value stored in
* the database.
*/
public void ejbLoad() throws RemoteException {
System.out.println("ejbLoad() called.");
/*
* Again, query the Entity Context to get the current
* Primary Key, so we know which instance to load.
*/
AccountPK pk = (AccountPK) ctx.getPrimaryKey();
String id = pk.accountID;
PreparedStatement pstmt = null;
Connection conn = null;
try {
/*
* 1) Acquire a new DB Connection
*/
conn = getConnection();
/*
* 2) Get account from the DB, querying
* by account 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 (SQLException ex) {
throw new RemoteException("Account " + pk + " failed to load from database", ex);
}
finally {
/*
* 3) Release the DB Connection
*/
try {
if (pstmt != null) pstmt.close();
}
catch (Exception e) { }
try {
if (conn != null) conn.close();
}
catch (Exception e) { }
}
}
/**
* Called from the Container. Updates the database
* to reflect the current values of this in-memory
* entity bean instance.
*/
public void ejbStore() throws RemoteException {
System.out.println("ejbStore() called.");
PreparedStatement pstmt = null;
Connection conn = null;
try {
/*
* 1) Acquire a new DB Connection
*/
conn = getConnection();
/*
* 2) Store account in DB
*/
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 (SQLException ex) {
throw new RemoteException("Account " + accountID + " failed to save to database", ex);
}
finally {
/*
* 3) Release the DB Connection
*/
try {
if (pstmt != null) pstmt.close();
}
catch (Exception e) { }
try {
if (conn != null) conn.close();
}
catch (Exception e) { }
}
}
/**
* Called by the container. Associates this bean
* instance with a particular context. We can query
* the bean properties which customize the bean here.
*/
public void setEntityContext(EntityContext ctx) throws RemoteException {
System.out.println("setEntityContext called");
this.ctx = ctx;
env = ctx.getEnvironment();
}
/**
* Called by Container. Disassociates this bean
* instance with a particular context environment.
*/
public void unsetEntityContext() throws RemoteException {
System.out.println("unsetEntityContext called");
this.ctx = null;
this.env = null;
}
/**
* Called after ejbCreate(). Now, the Bean can retrieve
* its EJBObject from its context, and pass it as
* a 'this' argument.
*/
public void ejbPostCreate(String accountID, String ownerName) throws RemoteException {
}
/**
* This is the initialization method that corresponds to the
* create() method in the Home Interface.
*
* When the client calls the Home Object's create() method,
* the Home Object then calls this ejbCreate() method.
*
* @return The primary key for this account
*/
public AccountPK ejbCreate(String accountID, String ownerName) throws CreateException, RemoteException {
PreparedStatement pstmt = null;
Connection conn = null;
try {
System.out.println("ejbCreate() called.");
this.accountID = accountID;
this.ownerName = ownerName;
this.balance = 0;
/*
* Acquire DB connection
*/
conn = getConnection();
/*
* Insert the account into the database
*/
pstmt = conn.prepareStatement("insert into accounts (id, ownerName, balance) values (?, ?, ?)");
pstmt.setString(1, accountID);
pstmt.setString(2, ownerName);
pstmt.setDouble(3, balance);
pstmt.executeUpdate();
/*
* Generate the Primary Key and return it
*/
return new AccountPK(accountID);
}
catch (Exception e) {
throw new CreateException(e.toString());
}
finally {
/*
* Release DB Connection for other beans
*/
try {
pstmt.close();
}
catch (Exception e) { }
try {
conn.close();
}
catch (Exception e) { }
}
}
/**
* Finds a Account by its primary Key
*/
public AccountPK ejbFindByPrimaryKey(AccountPK key) throws FinderException, RemoteException {
PreparedStatement pstmt = null;
Connection conn = null;
try {
System.out.println("ejbFindByPrimaryKey(" + key + ") called");
/*
* Acquire DB connection
*/
conn = getConnection();
/*
* Find the Entity in the DB
*/
pstmt = conn.prepareStatement("select id from accounts where id = ?");
pstmt.setString(1, key.toString());
ResultSet rs = pstmt.executeQuery();
rs.next();
/*
* No errors occurred, so return the Primary Key
*/
return key;
}
catch (Exception e) {
throw new FinderException(e.toString());
}
finally {
/*
* Release DB Connection for other beans
*/
try {
pstmt.close();
}
catch (Exception e) { }
try {
conn.close();
}
catch (Exception e) { }
}
}
/**
* Finds a Account by its Name
*/
public Enumeration ejbFindByOwnerName(String name) throws FinderException, RemoteException {
PreparedStatement pstmt = null;
Connection conn = null;
Vector v = new Vector();
try {
System.out.println("ejbFindByOwnerName(" + name + ") called");
/*
* Acquire DB connection
*/
conn = getConnection();
/*
* Find the primary keys in the DB
*/
pstmt = conn.prepareStatement("select id from accounts where ownerName = ?");
pstmt.setString(1, name);
ResultSet rs = pstmt.executeQuery();
/*
* Insert every primary key found into a vector
*/
while (rs.next()) {
String id = rs.getString("id");
v.addElement(new AccountPK(id));
}
/*
* Return an enumeration of found primary keys
*/
return v.elements();
}
catch (Exception e) {
throw new FinderException(e.toString());
}
finally {
/*
* Release DB Connection for other beans
*/
try {
pstmt.close();
}
catch (Exception e) { }
try {
conn.close();
}
catch (Exception e) { }
}
}
public static final String JDBC_URL = "JDBC_URL";
/**
* Gets current connection to the connection pool.
*
* Note: In the future, once vendors have adopted JDBC 2.0,
* the preferred method for retrieving a JDBC Connection is
* through JNDI.
*
* Assumes that environment properties contains a property
* named "user" and "password" if authentication is desired.
* These environment properties are retrieved from the
* EntityContext object during setEntityContext().
*
* @return Connection
* @exception java.sql.SQLException
*
*/
public Connection getConnection() throws SQLException {
String jdbcURL = (String) env.get(JDBC_URL);
// For debugging...
// String drivers = (String) env.get("jdbc.drivers");
// System.out.println(jdbcURL + ", " + drivers);
// DriverManager.setLogStream(System.out);
return DriverManager.getConnection(jdbcURL, env);
}
// static {
// try {
// new weblogic.jdbc.jts.Driver();
// Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// }
// catch (Exception e) {
// e.printStackTrace();
// }
// }
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -