FileDocCategorySizeDatePackage
ShipBean.javaAPI DocExample8177Thu Dec 16 17:17:54 GMT 1999com.titan.ship

ShipBean

public class ShipBean extends Object implements javax.ejb.EntityBean

Fields Summary
public int
id
public String
name
public int
capacity
public double
tonnage
public javax.ejb.EntityContext
context
Constructors Summary
Methods Summary
public voidejbActivate()

 
public ShipPKejbCreate(int id, java.lang.String name, int capacity, double tonnage)

        if((id < 1) || (name == null))
           throw new CreateException("Invalid Parameters");
        this.id = id;
        this.name = name;
        this.capacity = capacity;
        this.tonnage = tonnage;
        
        Connection con = null;
        PreparedStatement ps = null;
        try {
          con = this.getConnection();
          ps = con.prepareStatement(
           "insert into Ship (id, name, capacity, tonnage) " +
           "values (?,?,?,?)");
          ps.setInt(1, id);
          ps.setString(2, name);
          ps.setInt(3, capacity);
          ps.setDouble(4, tonnage);
          if (ps.executeUpdate() != 1) {
            throw new CreateException ("Failed to add Ship to database");
          }
          ShipPK primaryKey = new ShipPK();
          primaryKey.id = id;
          return primaryKey;
        }
        catch (SQLException se) {
          throw new EJBException(se);
        }
        finally {
          try {         
            if (ps != null) ps.close(); 
            if (con!= null) con.close(); 
          } catch(SQLException se){
            se.printStackTrace();
          }
        }
    
public ShipPKejbCreate(int id, java.lang.String name)

        return ejbCreate(id,name,0,0);
    
public java.util.EnumerationejbFindByCapacity(int capacity)

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet result = null;        
        try {
          con = this.getConnection();
          ps = con.prepareStatement(
              "select id from Ship where capacity = ?");
          ps.setInt(1,capacity);
          result = ps.executeQuery();
          Vector keys = new Vector();
          while(result.next()){
            ShipPK shipPk = new ShipPK();
            shipPk.id = result.getInt("id");
            keys.addElement(shipPk);
          }
          return keys.elements();
          
        }
        catch (SQLException se) {
          throw new EJBException(se);
        }
        finally {
          try {
            if (result != null) result.close();
            if (ps != null) ps.close(); 
            if (con!= null) con.close(); 
          } catch(SQLException se){
            se.printStackTrace();
          }
        }      
    
public ShipPKejbFindByPrimaryKey(ShipPK primaryKey)

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet result = null;
        try {
          con = this.getConnection();
          ps = con.prepareStatement(
              "select id from Ship where id = ?");
          ps.setInt(1, primaryKey.id);
          result = ps.executeQuery();
          // does ship id exist in database?
          if(!result.next()){
            throw new ObjectNotFoundException(
                "Cannot find Ship with id = "+id);
          }
        }catch (SQLException se) {
          throw new EJBException(se);
        }
        finally {
          try {
            if (result != null) result.close();
            if (ps != null) ps.close(); 
            if (con!= null) con.close(); 
          } catch(SQLException se){
            se.printStackTrace();
          }
        }
        return primaryKey;
    
public voidejbLoad()

         ShipPK pk = (ShipPK) context.getPrimaryKey();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet result = null;
        try {
          con = this.getConnection();
          ps = con.prepareStatement(
              "select name, capacity, tonnage from Ship where id = ?");
          ps.setInt(1,pk.id);
          result = ps.executeQuery();
          if(result.next()){
            id = id;
            name = result.getString("name");
            capacity = result.getInt("capacity");
            tonnage = result.getDouble("tonnage");
          } else{
            throw new EJBException(new ObjectNotFoundException(
                "Cannot find Ship with id = "+id));
          }
        }catch (SQLException se) {
          throw new EJBException(se);
        }
        finally {
          try {
            if (result != null) result.close();
            if (ps != null) ps.close(); 
            if (con!= null) con.close(); 
          } catch(SQLException se){
            se.printStackTrace();
          }
        }      
    
public voidejbPassivate()

 
public voidejbPostCreate(int id, java.lang.String name, int capacity, double tonnage)

        // Do something useful with the primary key.
    
public voidejbPostCreate(int id, java.lang.String name)

        // Do something useful with the EJBObject reference.
    
public voidejbRemove()

        Connection con = null;
        PreparedStatement ps = null;
        try {
          con = this.getConnection();
          ps = con.prepareStatement("delete from Ship where id = ?");
          ps.setInt(1, id);
          if (ps.executeUpdate() != 1) {
            throw new EJBException("remove failed");
          }
        }
        catch (SQLException se) {
          throw new EJBException(se);
        }
        finally {
          try {
            if (ps != null) ps.close(); 
            if (con!= null) con.close(); 
          } catch(SQLException se){
            se.printStackTrace();
          }
        }
    
public voidejbStore()

 
        Connection con = null;
        PreparedStatement ps = null;
        try {
          con = this.getConnection();
          ps = con.prepareStatement(
              "update Ship set name = ?, capacity = ?, " +
              "tonnage = ? where id = ?");
          ps.setString(1,name);
          ps.setInt(2,capacity);
          ps.setDouble(3,tonnage);
          ps.setInt(4,id);
          if (ps.executeUpdate() != 1) {
            throw new EJBException("ejbStore failed");
          }
        }
        catch (SQLException se) {
          throw new EJBException(se);
        }
        finally {
          try {
            if (ps != null) ps.close(); 
            if (con!= null) con.close(); 
          } catch(SQLException se){
            se.printStackTrace();
          }
        }
    
public intgetCapacity()

        return capacity;
    
private java.sql.ConnectiongetConnection()

      try {
        Context jndiCntx = new InitialContext();
        DataSource ds = 
            (DataSource)jndiCntx.lookup("java:comp/env/jdbc/titanDB");
        return ds.getConnection();
      }
      catch (NamingException ne) {
        throw new EJBException(ne);
      }
    
public java.lang.StringgetName()

        return name;
    
public doublegetTonnage()

        return tonnage;
    
public voidsetCapacity(int cap)

        capacity = cap;
    
public voidsetEntityContext(javax.ejb.EntityContext ctx)

        context = ctx;
    
public voidsetName(java.lang.String n)

        name = n;
    
public voidsetTonnage(double tons)

        tonnage = tons;
    
public voidunsetEntityContext()

        context = null;