这是一个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;
}
}
* $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;
}
}
解决方案 »
- 用spring开发非web应用的时候Spring MVC怎么运用?
- 求助poi加密Workbook的方法!
- 开发的平台出现唯一的错误 求解 高手进来
- 有点代码不懂给我解释下谢谢
- 在js中使用Struts2的标签问题
- 加载XML出错 Exception caught while loading book.xml: [object Error]
- 如何向SQLSERVER2000插入\修改\保存数据(向各位请教)
- 高分求Struts的入门教程
- 建立新EJB JAR时,提示:You must add class files for this enterprise bean
- 在使用ant 的过程中,遇到的问题?望高手能帮忙解决。
- 请问各位高手!怎样用Java从数据库中读出数据库记录以后保存到XML文件中?谢谢!20分必给!
- EJB的问题,希望用EJB做过开发的高手回答。
* $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;
}
}