The ShipBean

The ShipBean defined for this chapter uses JDBC to synchronize the bean’s state to the database. In reality, an entity bean this simple could easily be deployed as a CMP bean. The purpose of this chapter, however, is to illustrate exactly where the resource-access code goes for BMP and how to implement it. When learning about bean-managed persistence, you should focus on when and where the resource is accessed in order to synchronize the bean with the database. The fact that we are using JDBC and synchronizing the bean state against a relational database is not important. The bean could just as easily be persisted to some legacy system, to an ERP application, or to some other resource that is not supported by your vendor’s version of CMP.

Here is the complete definition of the ShipBean:

package com.titan.ship;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.ejb.EntityContext;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import javax.sql.DataSource;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.ObjectNotFoundException;
import java.util.Collection;
import java.util.Properties;
import java.util.Vector;
import java.util.Collection;

public class ShipBean implements javax.ejb.EntityBean {
    public Integer id;
    public String name;
    public int capacity;
    public double tonnage;

    public EntityContext context;
       
    public Integer ejbCreate(Integer id, String name, int capacity, double tonnage) 
        throws CreateException {
        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 {         
                if (ps != null) ps.close( ); 
                if (con!= null) con.close( ); 
            } catch(SQLException se) {
                se.printStackTrace( );
            }
        }
    }
    public void ejbPostCreate(Integer id, String name, int capacity, 
        double tonnage) {
        // Do something useful with the primary key.
    }
    public Integer ejbCreate(Integer id, String name) throws CreateException {
        return ejbCreate(id,name,0,0);
    }
    public void ejbPostCreate(Integer id, String name) {
        // Do something useful with the EJBObject reference.
    }
    public Integer ejbFindByPrimaryKey(Integer primaryKey) throws FinderException {
        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 the ship ID exist in the 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 Collection ejbFindByCapacity(int capacity) throws FinderException {
        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 {
                if (result != null) result.close( );
                if (ps != null) ps.close( ); 
                if (con!= null) con.close( ); 
            } catch(SQLException se) {
                se.printStackTrace( );
            }
        }      
    }
    public void setEntityContext(EntityContext ctx) {
        context = ctx;
    }
    public void unsetEntityContext( ) {
        context = null;
    }
    public void ejbActivate( ) {}
    public void ejbPassivate( ) {}
    public void ejbLoad( ) {

        Integer primaryKey = (Integer)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, 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 NoSuchEntityException( );
            }
        } 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 void ejbStore( ) {
        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 NoSuchEntityException("ejbStore");
            }
        }
        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 void ejbRemove( ) {
        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");
            }
        }
        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 String getName( ) {
        return name;
    }
    public void setName(String n) {
        name = n;
    }
    public void setCapacity(int cap) {
        capacity = cap;
    }
    public int getCapacity( ) {
        return capacity;
    }
    public double getTonnage( ) {
        return tonnage;
    }
    public void setTonnage(double tons) {
        tonnage = tons;
    }
    private Connection getConnection( ) throws SQLException {
         // Implementations shown below.
   }
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.104.124