⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 entitybeantest.java

📁 JAVA编程百例书中各章节的所有例子的源代码,包括套接字编程
💻 JAVA
字号:
package ch05.section08;

import java.sql.*;
import javax.naming.*;
import javax.ejb.*;
import java.util.*;
import java.rmi.RemoteException;

public class EntityBeanTest
    implements EntityBean {
  protected EntityContext ctx;
  public String accountID;
  public String ownerName;
  public double balance;
  public Properties env;
  public EntityBeanTest() {
  }

  public void deposit(double amt) throws Exception {
    System.out.println("deposit(" + amt + ") called.");
    balance += amt;
  }

  public void withdraw(double amt) throws Exception {
    System.out.println("withdraw(" + amt + ") called.");
    if (amt > balance) {
      throw new Exception("Your balance is " + balance +
                          "!  You cannot withdraw " + amt + "!");
    }
    balance -= amt;
  }

  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;
  }

  public void ejbActivate() throws RemoteException {
    System.out.println("ejbActivate() called.");
  }

  public void ejbRemove() throws RemoteException {
    System.out.println("ejbRemove() called.");
    PKClass pk = (PKClass) ctx.getPrimaryKey();
    String id = pk.accountID;
    PreparedStatement pstmt = null;
    Connection conn = null;
    try {
      conn = getConnection();
      pstmt = conn.prepareStatement("delete from accounts where id = ?");
      pstmt.setString(1, id);
      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 {
      try {
        if (pstmt != null) {
          pstmt.close();
        }
      }
      catch (Exception e) {}
      try {
        if (conn != null) {
          conn.close();
        }
      }
      catch (Exception e) {}
    }
  }

  public void ejbPassivate() throws RemoteException {
    System.out.println("ejbPassivate () called.");
  }

  public void ejbLoad() throws RemoteException {
    System.out.println("ejbLoad() called.");
    PKClass pk = (PKClass) ctx.getPrimaryKey();
    String id = pk.accountID;
    PreparedStatement pstmt = null;
    Connection conn = null;
    try {
      conn = getConnection();
      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 {
      try {
        if (pstmt != null) {
          pstmt.close();
        }
      }
      catch (Exception e) {}
      try {
        if (conn != null) {
          conn.close();
        }
      }
      catch (Exception e) {}
    }
  }

  public void ejbStore() throws RemoteException {
    System.out.println("ejbStore() called.");
    PreparedStatement pstmt = null;
    Connection conn = null;
    try {
      conn = getConnection();
      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 {
      try {
        if (pstmt != null) {
          pstmt.close();
        }
      }
      catch (Exception e) {}
      try {
        if (conn != null) {
          conn.close();
        }
      }
      catch (Exception e) {}
    }
  }

  public void setEntityContext(EntityContext ctx) throws RemoteException {
    System.out.println("setEntityContext called");
    this.ctx = ctx;
    env = ctx.getEnvironment();
  }

  public void unsetEntityContext() throws RemoteException {
    System.out.println("unsetEntityContext called");
    this.ctx = null;
    this.env = null;
  }

  public void ejbPostCreate(String accountID, String ownerName) throws
      RemoteException {
  }

  public PKClass 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;
      conn = getConnection();
      pstmt = conn.prepareStatement(
          "insert into accounts (id, ownerName, balance) values (?, ?, ?)");
      pstmt.setString(1, accountID);
      pstmt.setString(2, ownerName);
      pstmt.setDouble(3, balance);
      pstmt.executeUpdate();
      return new PKClass(accountID);
    }
    catch (Exception e) {
      throw new CreateException(e.toString());
    }
    finally {
      try {
        pstmt.close();
      }
      catch (Exception e) {}
      try {
        conn.close();
      }
      catch (Exception e) {}
    }
  }

  public PKClass ejbFindByPrimaryKey(PKClass key) throws FinderException,
      RemoteException {
    PreparedStatement pstmt = null;
    Connection conn = null;
    try {
      System.out.println("ejbFindByPrimaryKey(" + key + ") called");
      conn = getConnection();
      pstmt = conn.prepareStatement("select id from accounts where id = ?");
      pstmt.setString(1, key.toString());
      ResultSet rs = pstmt.executeQuery();
      rs.next();
      return key;
    }
    catch (Exception e) {
      throw new FinderException(e.toString());
    }
    finally {
      try {
        pstmt.close();
      }
      catch (Exception e) {}
      try {
        conn.close();
      }
      catch (Exception e) {}
    }
  }

  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");
      conn = getConnection();
      pstmt = conn.prepareStatement(
          "select id from accounts where ownerName = ?");
      pstmt.setString(1, name);
      ResultSet rs = pstmt.executeQuery();
      while (rs.next()) {
        String id = rs.getString("id");
        v.addElement(new PKClass(id));
      }
      return v.elements();
    }
    catch (Exception e) {
      throw new FinderException(e.toString());
    }
    finally {
      try {
        pstmt.close();
      }
      catch (Exception e) {}
      try {
        conn.close();
      }
      catch (Exception e) {}
    }
  }

  public static final String JDBC_URL = "JDBC_URL";
  public Connection getConnection() throws SQLException {
    String jdbcURL = (String) env.get(JDBC_URL);
    return DriverManager.getConnection(jdbcURL, env);
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -