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

📄 savingsaccount.java

📁 一本关于JBuilder 应用开发的书籍,希望大家喜欢,其实我没看过的,
💻 JAVA
字号:
// SavingsAccount.java
package bank;
import java.rmi.*;
import java.sql.*;
import java.util.*;

import javax.ejb.*;
import javax.naming.*;
import javax.sql.*;

public class SavingsAccount implements EntityBean {

  private EntityContext _context;
  private DataSource _dataSource;

  private String _name;
  private float _balance;

  public float getBalance()
  {
    return _balance;
  }

  public void debit(float amount)
  {
    if(amount > _balance)
    {
      //余额不足的处理
      _context.setRollbackOnly();
    }
    else
    {
      _balance = _balance - amount;
    }
  }

  public void credit(float amount)
  {
    _balance = _balance + amount;
  }

  public void setEntityContext(EntityContext context)
  {
    _context = context;
  }

  public void unsetEntityContext()
  {
    _context = null;
  }

  public void ejbActivate()
  {
  }

  public void ejbPassivate()
  {
  }

  public AccountPK ejbCreate(String name, float balance) throws RemoteException, CreateException
  {
    try
    {
      
      ejbFindByPrimaryKey(new AccountPK(name));
      
      throw new DuplicateKeyException();
    }
    catch(ObjectNotFoundException e)
    {
      
    }
    _name = name;
    _balance = balance;
    Connection connection = null;
    PreparedStatement statement = null;
    try {
      connection = getConnection();
      statement = connection.prepareStatement("INSERT INTO Savings_Accounts (name, balance) VALUES (?, ?)");
      statement.setString(1, _name);
      statement.setFloat(2, _balance);
      if(statement.executeUpdate() != 1)
      {
        throw new CreateException("Could not create: " + name);
      }
      return new AccountPK(name);
    }
    catch(SQLException e)
    {
      throw new RemoteException("Could not create: " + name, e);
    }
    finally
    {
      try
      {
        if (statement != null)
        {
          statement.close();
        }
        if (connection != null)
        {
          connection.close();
        }
      }
      catch (SQLException sqe)
      {
        throw new RemoteException("Could not create, close statement, connection: " + name, sqe);
      }
    }
  }

  public void ejbPostCreate(String name, float balance) throws RemoteException, CreateException
  {
  }

  public void ejbRemove() throws RemoteException, RemoveException
  {
    Connection connection = null;
    PreparedStatement statement = null;
    try
    {
      connection = getConnection();
      statement = connection.prepareStatement("DELETE FROM Savings_Accounts WHERE name = ?");
      statement.setString(1, _name);
      if(statement.executeUpdate() < 1)
      {
        throw new RemoveException("Could not remove: " + _name);
      }
    }
    catch(SQLException e)
    {
      throw new RemoteException("Could not remove: " + _name, e);
    }
    finally
    {
      try
      {
        if (statement != null)
        {
          statement.close();
        }
        if (connection != null)
        {
          connection.close();
        }
      }
      catch (SQLException sqe)
      {
        throw new RemoteException("Could not statement, connection: ", sqe);
      }
    }

  }

  public AccountPK ejbFindByPrimaryKey(AccountPK key)   throws RemoteException, ObjectNotFoundException
  {
    Connection connection = null;
    PreparedStatement statement = null;
    try
    {
      connection = getConnection();
      statement = connection.prepareStatement
        ("SELECT name FROM Savings_Accounts WHERE name = ?");
      statement.setString(1, key.name);
      ResultSet resultSet = statement.executeQuery();
      if(!resultSet.next())
      {
        throw new ObjectNotFoundException("Could not find: " + key);
      }
      return key;
    }
    catch(SQLException e)
    {
      throw new RemoteException("Could not find: " + key, e);
    }
    finally
    {
      try
      {
        if (statement != null)
        {
          statement.close();
        }
        if (connection != null)
        {
          connection.close();
        }
      }
      catch (SQLException sqe)
      {
        throw new RemoteException("Could not find: " + key, sqe);
      }
    }
  }

  public Enumeration ejbFindAccountsLargerThan(float balance) throws RemoteException, FinderException
  {
    Connection connection = null;
    PreparedStatement statement = null;
    try
    {
      connection = getConnection();
      statement = connection.prepareStatement("SELECT name FROM Savings_Accounts WHERE balance > ?");
      statement.setFloat(1, balance);
      ResultSet resultSet = statement.executeQuery();
      Vector keys = new Vector();
      while(resultSet.next())
      {
        String name = resultSet.getString(1);
        keys.addElement(new AccountPK(name));
      }
      return keys.elements();

    }
    catch(SQLException e)
    {
      throw new RemoteException("Could not findAccountsLargerThan: " + balance, e);
    }
    finally
    {
      try
      {
        if (statement != null)
        {
          statement.close();
        }
        if (connection != null)
        {
          connection.close();
        }
      }
      catch (SQLException sqe)
      {
        throw new RemoteException("Could not findAccountsLargerThan: " + balance, sqe);
      }
    }
  }

  public void ejbLoad() throws RemoteException
  {
   
    _name = ((AccountPK) _context.getPrimaryKey()).name;
    Connection connection = null;
    PreparedStatement statement = null;
    try
    {
      connection = getConnection();
      statement = connection.prepareStatement("SELECT balance FROM Savings_Accounts WHERE name = ?");
      statement.setString(1, _name);
      ResultSet resultSet = statement.executeQuery();
      if(!resultSet.next())
      {
        throw new RemoteException("Account not found: " + _name);
      }
      _balance = resultSet.getFloat(1);
    }
    catch(SQLException e)
    {
      throw new RemoteException("Could not load: " + _name, e);
    }
    finally
    {
      try
      {
        if (statement != null)
        {
          statement.close();
        }
        if (connection != null)
        {
          connection.close();
        }
      }
      catch (SQLException sqe)
      {
        throw new RemoteException("Could not load: " + _name, sqe);
      }
    }

  }

  public void ejbStore() throws RemoteException
  {
    Connection connection = null;
    PreparedStatement statement = null;
    try
    {
      connection = getConnection();
      statement = connection.prepareStatement
        ("UPDATE Savings_Accounts SET balance = ? WHERE name = ?");
      statement.setFloat(1, _balance);
      statement.setString(2, _name);
      statement.executeUpdate();
    }
    catch(SQLException e)
    {
      throw new RemoteException("Could not store: " + _name, e);
    }
    finally
    {
      try
      {
        if (statement != null)
        {
          statement.close();
        }
        if (connection != null)
        {
          connection.close();
        }
      }
      catch (SQLException sqe)
      {
        throw new RemoteException("Could not store: " + _name, sqe);
      }
    }

  }

  private Connection getConnection() throws SQLException, RemoteException
  {
    if(_dataSource == null)
    {
      try
      {
        Context context = new InitialContext();
        _dataSource = (DataSource) context.lookup("java:comp/env/jdbc/SavingsDataSource");
      }
      catch(NamingException e)
      {
        throw new RemoteException("Could not obtain DataSource: " + e);
      }
    }
    return _dataSource.getConnection();
  }

  public String toString()
  {
    return "SavingsAccount[name=" + _name + ",balance=" + _balance +"]";
  }

}

⌨️ 快捷键说明

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