package examples.ejb.basic.beanManaged; import java.io.Serializable; import java.rmi.RemoteException; import javax.ejb.*; import java.util.*; import java.sql.*; /** * AccountBean is an EntityBean. This EJBean illustrates: *
AccountHome.create()
, the container (which in WebLogic EJB is
* also the home) allocates an instance of this bean and
* calls AccountBean.ejbCreate()
.
*
* For bean-managed persistence, ejbCreate()
returns
* a primary key, unlike the case of container-managed
* persistence, where it returns a void.
*
* @param accountID String Account ID
* @param initialBalance double Initial Balance
* @return AccountPK
* @exception javax.ejb.CreateException
* if there is a problem creating the bean
* @exception java.rmi.RemoteException
* if there is a communications or systems failure
*/
public AccountPK ejbCreate(String accountId, double initialBalance)
throws CreateException,
RemoteException
{
if (VERBOSE)
System.out.println("AccountBean.ejbCreate( id = " + System.identityHashCode(this) + ", PK = " +
accountId + ", " + "initial balance = $ " + initialBalance + ")");
this.accountId = accountId;
this.balance = initialBalance;
Connection con = null;
PreparedStatement ps = null;;
try {
con = getConnection();
ps = con.prepareStatement("insert into ejbAccounts (id, bal) values (?, ?)");
ps.setString(1, accountId);
ps.setDouble(2, balance);
if (ps.executeUpdate() != 1) {
throw new CreateException ("JDBC did not create any row");
}
AccountPK primaryKey = new AccountPK();
primaryKey.accountId = accountId;
return primaryKey;
}
catch (CreateException ce) {
throw ce;
}
catch (SQLException sqe) {
throw new CreateException (sqe.getMessage());
}
finally {
try {
ps.close();
con.close();
}
catch (Exception e) {
throw new RemoteException (e.getMessage());
}
}
}
/**
* Required by the EJB specification, this method is not used
* by this example.
*
* @param accountID String Account Identification
* @param initialBalance double Initial Balance
*/
public void ejbPostCreate(String accountId, double initialBalance) {
if (VERBOSE)
System.out.println("ejbPostCreate (" + id() + ")");
}
// Application defined methods
/**
* Finds all EJBeans with a balance greater than a given amount.
* Returns an Enumeration of found EJBean primary keys.
*
* @param balanceGreaterThan double Test Amount
* @return Enumeration EJBean Primary Keys
* @exception javax.ejb.FinderException
* if an error occurs while accessing
* the persistent storage
* @exception java.rmi.RemoteException
* if there is a communications or systems failure
*/
public Enumeration ejbFindBigAccounts(double balanceGreaterThan)
throws FinderException,
RemoteException
{
if (VERBOSE)
System.out.println("ejbFindBigAccounts (balance > " +
balanceGreaterThan + ")");
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
con = getConnection();
ps = con.prepareStatement("select id, bal from ejbAccounts where bal > ?");
ps.setDouble(1, balanceGreaterThan);
ps.executeQuery();
rs = ps.getResultSet();
Vector v = new Vector();
AccountPK pk;
while (rs.next()) {
pk = new AccountPK();
pk.accountId = rs.getString(1);
v.addElement(pk);
}
rs.close();
return v.elements();
}
catch (SQLException sqe) {
throw new FinderException (sqe.getMessage());
}
finally {
try {
if (rs != null) rs.close();
if (ps != null) ps.close();
if (con!= null) con.close();
}
catch (Exception e) {
throw new RemoteException (e.getMessage());
}
}
}
/**
* Throws a FinderException as there is no savings account.
*
* @return AccountPK EJBean primary key
* @exception javax.ejb.FinderException
* if there is a problem finding the bean
*/
public AccountPK ejbFindSavingsAccount()
throws FinderException
{
throw new FinderException ("No savings account");
}
/**
* Adds amount to balance.
*
* @param amount double Amount
* @return double balance
* @exception ProcessingErrorException
* if there is an error making a deposit
*/
public double deposit(double amount)
throws ProcessingErrorException
{
if (VERBOSE)
System.out.println("Depositing $" + amount + " into '" + accountId + "'");
balance += amount;
return balance;
}
/**
* Subtracts amount from balance.
*
* @param amount double Amount
* @return double Balance
* @exception ProcessingErrorException
* if Amount > Balance.
*/
public double withdraw(double amount)
throws ProcessingErrorException
{
if (VERBOSE)
System.out.println("Withdrawing $" + amount + " from '" + accountId + "'");
if (amount > balance) {
throw new ProcessingErrorException("Request to withdraw $" + amount +
" more than balance " + balance +
" in account " + accountId);
}
balance -= amount;
return balance;
}
/**
* Returns current balance.
*
* @return double Balance
*/
public double balance() {
return balance;
}
/**
* Forces static initialization of weblogic.jdbc.jts.Driver
*
*/
static {
new weblogic.jdbc.jts.Driver(); // force static initialization
}
/**
* Gets current connection to the connection pool.
*
* @return Connection
* @exception java.sql.SQLException
* if there is an error getting a Connection
*/
public Connection getConnection()
throws SQLException
{
return DriverManager.getConnection("jdbc:weblogic:jts:demoPool");
}
}