FileDocCategorySizeDatePackage
ShipBean.javaAPI DocExample9928Mon May 13 18:22:28 BST 2002com.titan.ship

ShipBean

public class ShipBean extends Object implements javax.ejb.EntityBean

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

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

      
      System.out.println ("ejbCreate() pk="+id+" name="+name);
      
      if ((id.intValue () < 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.intValue ());
         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");
         }
         
         return id;
      }
      catch (SQLException se)
      {
         throw new EJBException (se);
      }
      finally
      {
         try { ps.close (); } catch (Exception e) {}
         try { con.close (); } catch (Exception e) {}
      }
   
public java.lang.IntegerejbCreate(java.lang.Integer id, java.lang.String name)

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

      
      System.out.println ("ejbFindByCapacity() capacity="+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 ())
         {
            keys.addElement (result.getObject ("id"));
         }
         return keys;
         
      }
      catch (SQLException se)
      {
         throw new EJBException (se);
      }
      finally
      {
         try { result.close (); } catch (Exception e) {}
         try { ps.close (); } catch (Exception e) {}
         try { con.close (); } catch (Exception e) {}
      }
   
public java.lang.IntegerejbFindByPrimaryKey(java.lang.Integer primaryKey)

      
      System.out.println ("ejbFindByPrimaryKey() primaryKey="+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.intValue ());
         
         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 { result.close (); } catch (Exception e) {}
         try { ps.close (); } catch (Exception e) {}
         try { con.close (); } catch (Exception e) {}
      }
      return primaryKey;
   
public voidejbHomeDeleteDbTable()

      PreparedStatement ps = null;
      Connection con = null;
      
      try
      {
         con = this.getConnection ();
         
         System.out.println("Dropping table SHIP...");
         ps = con.prepareStatement ("DROP TABLE SHIP");
         ps.execute ();
         System.out.println("...done!");
      }
      finally
      {
         try { ps.close (); } catch (Exception e) {}
         try { con.close (); } catch (Exception e) {}
      }
   
public voidejbHomeMakeDbTable()

      PreparedStatement ps = null;
      Connection con = null;
      
      try
      {
         con = this.getConnection ();
         
         System.out.println("Creating table SHIP...");
         ps = con.prepareStatement ("CREATE TABLE SHIP ( " +
         "ID INT PRIMARY KEY, " +
         "NAME CHAR(30), " +
         "TONNAGE DECIMAL (8,2), " +
         "CAPACITY INT" +
         ")" );
         ps.execute ();
         System.out.println("...done!");
      }
      finally
      {
         try { ps.close (); } catch (Exception e) {}
         try { con.close (); } catch (Exception e) {}
      }
   
public voidejbLoad()

      
      Integer primaryKey = (Integer)context.getPrimaryKey ();
      System.out.println ("ejbLoad() pk="+primaryKey);
      
      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, primaryKey.intValue ());
         result = ps.executeQuery ();
         
         if (result.next ())
         {
            id = primaryKey;
            name = result.getString ("name");
            capacity = result.getInt ("capacity");
            tonnage = result.getDouble ("tonnage");
         } else
         {
            throw new EJBException ();
         }
      } catch (SQLException se)
      {
         throw new EJBException (se);
      }
      finally
      {
         try { result.close (); } catch (Exception e) {}
         try { ps.close (); } catch (Exception e) {}
         try { con.close (); } catch (Exception e) {}
      }
   
public voidejbPassivate()

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

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

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

      
      System.out.println ("ejbRemove() pk="+id);

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

      
      System.out.println ("ejbStore() pk="+id);

      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.intValue ());
         
         if (ps.executeUpdate () != 1)
         {
            throw new EJBException ("ejbStore unable to update table");
         }
      }
      catch (SQLException se)
      {
         throw new EJBException (se);
      }
      finally
      {
         try { ps.close (); } catch (Exception e) {}
         try { con.close (); } catch (Exception e) {}
      }
   
public intgetCapacity()

      System.out.println ("getCapacity()");
      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()

      System.out.println ("getName()");
      return name;
   
public doublegetTonnage()

      System.out.println ("getTonnage()");
      return tonnage;
   
public voidsetCapacity(int cap)

      System.out.println ("setCapacity()");
      capacity = cap;
   
public voidsetEntityContext(javax.ejb.EntityContext ctx)

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

      System.out.println ("setName()");
      name = n;
   
public voidsetTonnage(double tons)

      System.out.println ("setTonnage()");
      tonnage = tons;
   
public voidunsetEntityContext()

      context = null;