这是一个EntityBean(BMP)的例子是apusic的历程/*
 * $Id: BMAccountBean.java,v 1.2 1999/11/04 00:08:07 yuan Exp $
 *
 * Copyright (C) 2000 by Apusic Corporation
 * Copyright (C) 1999-2000 by Daniel Yuan
 * All rights reserved
 */package samples.eb;import javax.ejb.*;
import javax.naming.*;
import java.sql.*;
import javax.sql.DataSource;
import java.rmi.RemoteException;
import java.util.Enumeration;
import java.util.Vector;
import java.util.Properties;public class BMAccountBean implements EntityBean
{
    protected static DataSource dataSource = null;
    protected transient EntityContext entityContext;
    protected transient boolean dirty;    // Object states
    public int accno;
    public String customer;
    public double balance;    public AccountPK ejbCreate(int accno, String customer, double balance)
throws RemoteException
    {
this.accno = accno;
this.customer = customer;
this.balance = balance; Connection conn = null;
PreparedStatement stmt = null;
try {
    conn = dataSource.getConnection();
    stmt = conn.prepareStatement("insert into account (accno, customer, balance) values (?, ?, ?)");
    stmt.setInt(1, accno);
    stmt.setString(2, customer);
    stmt.setDouble(3, balance);
    stmt.executeUpdate();
} catch (SQLException e) {
    throw new RemoteException("Failed to create account", e);
} finally {
    try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
    } catch (Exception e) {
// ignore
    }
} return new AccountPK(accno);
    }    public void ejbPostCreate(int accno, String customer, double balance)
throws RemoteException
    {
// nothing done
    }    public void ejbActivate() throws RemoteException {
// nothing done
    }    public void ejbPassivate() throws RemoteException {
// nothing done
    }    public void ejbLoad() throws RemoteException {
AccountPK pk = (AccountPK) entityContext.getPrimaryKey(); Connection conn = null;
PreparedStatement stmt = null;
try {
    conn = dataSource.getConnection();
    stmt = conn.prepareStatement("select customer, balance from account where accno = ?");
    stmt.setInt(1, pk.accno);
    ResultSet rs = stmt.executeQuery();
    if (rs.next() == false) {
throw new RemoteException("Failed to load account from database");
    }     accno = pk.accno;
    customer = rs.getString("customer");
    balance = rs.getDouble("balance");
} catch (SQLException e) {
    throw new RemoteException("Failed to load account from database");
} finally {
    try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
    } catch (Exception e) {
// ignore
    }
}
    }    public void ejbStore() throws RemoteException {
Connection conn = null;
PreparedStatement stmt = null; try {
    conn = dataSource.getConnection();
    stmt = conn.prepareStatement("update account set customer=?, balance=? where accno=?");
    stmt.setString(1, customer);
    stmt.setDouble(2, balance);
    stmt.setInt(3, accno);
    stmt.executeUpdate();
} catch (SQLException e) {
    throw new RemoteException("Failed to store account to database", e);
} finally {
    try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
    } catch (Exception e) {
// ignore
    }
}
    }    public void ejbRemove() throws RemoteException, RemoveException {
AccountPK pk = (AccountPK) entityContext.getPrimaryKey(); Connection conn = null;
PreparedStatement stmt = null;
try {
    conn = dataSource.getConnection();
    stmt = conn.prepareStatement("delete from account where accno=?");
    stmt.setInt(1, pk.accno);
    stmt.executeUpdate();
} catch (SQLException e) {
    throw new RemoteException("Failed to delete account from database", e);
} finally {
    try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
    } catch (Exception e) {
// ignore
    }
}
    }    public AccountPK ejbFindByPrimaryKey(AccountPK pk)
throws ObjectNotFoundException, RemoteException
    {
Connection conn = null;
PreparedStatement stmt = null;
try {
    conn = dataSource.getConnection();
    stmt = conn.prepareStatement("select accno from account where accno=?");
    stmt.setInt(1, pk.accno);
    ResultSet rs = stmt.executeQuery();
    if (rs.next() == false) {
throw new ObjectNotFoundException();
    }
} catch (SQLException e) {
    throw new RemoteException("Failed to find account", e);
} finally {
    try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
    } catch (Exception e) {
// ignore
    }
} return pk;
    }    public AccountPK ejbFindByNumber(int accno)
throws ObjectNotFoundException, RemoteException
    {
Connection conn = null;
PreparedStatement stmt = null;
try {
    conn = dataSource.getConnection();
    stmt = conn.prepareStatement("select accno from account where accno=?");
    stmt.setInt(1, accno);
    ResultSet rs = stmt.executeQuery();
    if (rs.next() == false) {
throw new ObjectNotFoundException();
    }
} catch (SQLException e) {
    throw new RemoteException("Failed to find account", e);
} finally {
    try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
    } catch (Exception e) {
// ignore
    }
} return new AccountPK(accno);
    }    public Enumeration ejbFindAllAccounts()
throws ObjectNotFoundException, RemoteException
    {
Vector v = new Vector();
Connection conn = null;
PreparedStatement stmt = null;
try {
    conn = dataSource.getConnection();
    stmt = conn.prepareStatement("select accno from account");
    ResultSet rs = stmt.executeQuery();
    if (rs.next() == false) {
throw new ObjectNotFoundException("Empty account list");
    }     do {
AccountPK pk = new AccountPK(rs.getInt("accno"));
v.addElement(pk);
    } while (rs.next());
} catch (SQLException e) {
    throw new RemoteException("Failed to find accounts", e);
} finally {
    try {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
    } catch (Exception e) {
// ignore
    }
} return v.elements();
    }    public void setEntityContext(EntityContext ctx) throws RemoteException {
entityContext = ctx; if (dataSource == null) {
    Context initCtx = null;
    try {
initCtx = new InitialContext();
    } catch (Exception e) {
throw new RemoteException("Cannot get InitialContext", e);
    }     try {
dataSource = (DataSource) initCtx.lookup("java:comp/env/jdbc/sample");
    } catch (Exception e) {
throw new RemoteException("Cannot lookup data source", e);
    }
}
    }    public void unsetEntityContext() {
entityContext = null;
    }    public int getNumber() throws RemoteException {
return accno;
    }    public String getCustomer() throws RemoteException {
return customer;
    }    public void setCustomer(String customer) throws RemoteException {
this.customer = customer;
setDirty(true);
    }    public double getBalance() throws RemoteException {
return balance;
    }    public double deposit(double amount) throws RemoteException {
balance += amount;
setDirty(true);
return balance;
    }    public double withdraw(double amount) throws RemoteException {
if (balance < amount)
    throw new RemoteException("No enough balance");
balance -= amount;
setDirty(true);
return balance;
    }    private void setDirty(boolean flag) {
dirty = flag;
    }    public boolean isModified() {
return dirty;
    }
}

解决方案 »

  1.   

    这是一个EntityBean(BMP)的例子是apusic的历程你可以到www.apusic.com找到更多的相关资料/*
     * $Id: BMAccountBean.java,v 1.2 1999/11/04 00:08:07 yuan Exp $
     *
     * Copyright (C) 2000 by Apusic Corporation
     * Copyright (C) 1999-2000 by Daniel Yuan
     * All rights reserved
     */package samples.eb;import javax.ejb.*;
    import javax.naming.*;
    import java.sql.*;
    import javax.sql.DataSource;
    import java.rmi.RemoteException;
    import java.util.Enumeration;
    import java.util.Vector;
    import java.util.Properties;public class BMAccountBean implements EntityBean
    {
        protected static DataSource dataSource = null;
        protected transient EntityContext entityContext;
        protected transient boolean dirty;    // Object states
        public int accno;
        public String customer;
        public double balance;    public AccountPK ejbCreate(int accno, String customer, double balance)
    throws RemoteException
        {
    this.accno = accno;
    this.customer = customer;
    this.balance = balance; Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement("insert into account (accno, customer, balance) values (?, ?, ?)");
        stmt.setInt(1, accno);
        stmt.setString(2, customer);
        stmt.setDouble(3, balance);
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw new RemoteException("Failed to create account", e);
    } finally {
        try {
    if (stmt != null) stmt.close();
    if (conn != null) conn.close();
        } catch (Exception e) {
    // ignore
        }
    } return new AccountPK(accno);
        }    public void ejbPostCreate(int accno, String customer, double balance)
    throws RemoteException
        {
    // nothing done
        }    public void ejbActivate() throws RemoteException {
    // nothing done
        }    public void ejbPassivate() throws RemoteException {
    // nothing done
        }    public void ejbLoad() throws RemoteException {
    AccountPK pk = (AccountPK) entityContext.getPrimaryKey(); Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement("select customer, balance from account where accno = ?");
        stmt.setInt(1, pk.accno);
        ResultSet rs = stmt.executeQuery();
        if (rs.next() == false) {
    throw new RemoteException("Failed to load account from database");
        }     accno = pk.accno;
        customer = rs.getString("customer");
        balance = rs.getDouble("balance");
    } catch (SQLException e) {
        throw new RemoteException("Failed to load account from database");
    } finally {
        try {
    if (stmt != null) stmt.close();
    if (conn != null) conn.close();
        } catch (Exception e) {
    // ignore
        }
    }
        }    public void ejbStore() throws RemoteException {
    Connection conn = null;
    PreparedStatement stmt = null; try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement("update account set customer=?, balance=? where accno=?");
        stmt.setString(1, customer);
        stmt.setDouble(2, balance);
        stmt.setInt(3, accno);
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw new RemoteException("Failed to store account to database", e);
    } finally {
        try {
    if (stmt != null) stmt.close();
    if (conn != null) conn.close();
        } catch (Exception e) {
    // ignore
        }
    }
        }    public void ejbRemove() throws RemoteException, RemoveException {
    AccountPK pk = (AccountPK) entityContext.getPrimaryKey(); Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement("delete from account where accno=?");
        stmt.setInt(1, pk.accno);
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw new RemoteException("Failed to delete account from database", e);
    } finally {
        try {
    if (stmt != null) stmt.close();
    if (conn != null) conn.close();
        } catch (Exception e) {
    // ignore
        }
    }
        }    public AccountPK ejbFindByPrimaryKey(AccountPK pk)
    throws ObjectNotFoundException, RemoteException
        {
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement("select accno from account where accno=?");
        stmt.setInt(1, pk.accno);
        ResultSet rs = stmt.executeQuery();
        if (rs.next() == false) {
    throw new ObjectNotFoundException();
        }
    } catch (SQLException e) {
        throw new RemoteException("Failed to find account", e);
    } finally {
        try {
    if (stmt != null) stmt.close();
    if (conn != null) conn.close();
        } catch (Exception e) {
    // ignore
        }
    } return pk;
        }    public AccountPK ejbFindByNumber(int accno)
    throws ObjectNotFoundException, RemoteException
        {
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement("select accno from account where accno=?");
        stmt.setInt(1, accno);
        ResultSet rs = stmt.executeQuery();
        if (rs.next() == false) {
    throw new ObjectNotFoundException();
        }
    } catch (SQLException e) {
        throw new RemoteException("Failed to find account", e);
    } finally {
        try {
    if (stmt != null) stmt.close();
    if (conn != null) conn.close();
        } catch (Exception e) {
    // ignore
        }
    } return new AccountPK(accno);
        }    public Enumeration ejbFindAllAccounts()
    throws ObjectNotFoundException, RemoteException
        {
    Vector v = new Vector();
    Connection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = dataSource.getConnection();
        stmt = conn.prepareStatement("select accno from account");
        ResultSet rs = stmt.executeQuery();
        if (rs.next() == false) {
    throw new ObjectNotFoundException("Empty account list");
        }     do {
    AccountPK pk = new AccountPK(rs.getInt("accno"));
    v.addElement(pk);
        } while (rs.next());
    } catch (SQLException e) {
        throw new RemoteException("Failed to find accounts", e);
    } finally {
        try {
    if (stmt != null) stmt.close();
    if (conn != null) conn.close();
        } catch (Exception e) {
    // ignore
        }
    } return v.elements();
        }    public void setEntityContext(EntityContext ctx) throws RemoteException {
    entityContext = ctx; if (dataSource == null) {
        Context initCtx = null;
        try {
    initCtx = new InitialContext();
        } catch (Exception e) {
    throw new RemoteException("Cannot get InitialContext", e);
        }     try {
    dataSource = (DataSource) initCtx.lookup("java:comp/env/jdbc/sample");
        } catch (Exception e) {
    throw new RemoteException("Cannot lookup data source", e);
        }
    }
        }    public void unsetEntityContext() {
    entityContext = null;
        }    public int getNumber() throws RemoteException {
    return accno;
        }    public String getCustomer() throws RemoteException {
    return customer;
        }    public void setCustomer(String customer) throws RemoteException {
    this.customer = customer;
    setDirty(true);
        }    public double getBalance() throws RemoteException {
    return balance;
        }    public double deposit(double amount) throws RemoteException {
    balance += amount;
    setDirty(true);
    return balance;
        }    public double withdraw(double amount) throws RemoteException {
    if (balance < amount)
        throw new RemoteException("No enough balance");
    balance -= amount;
    setDirty(true);
    return balance;
        }    private void setDirty(boolean flag) {
    dirty = flag;
        }    public boolean isModified() {
    return dirty;
        }
    }