///////////////////////////////////////////////////////////////////////////////
// File     : EJB_ADDRESSBean.java
// Creation : 2002.04.29 (generated by ejen 1.0.0)

package org.ejb.test;

import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.ObjectNotFoundException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Vector;
import java.util.Collection;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * Entity bean class that maps the ADDRESS table.
 * <br><i>Ejen EJB 1.1 (BMP) demonstration</i>
 * @version 1.0
 * @author (unknown)
 * @see org.ejb.test.ADDRESSValues
 * @see org.ejb.test.EJB_ADDRESSHome
 * @see org.ejb.test.EJB_ADDRESS
 * @see org.ejb.test.EJB_ADDRESS_PK
 * @see org.ejb.test.EJB_TABLEAbstractBean
 */
public class EJB_ADDRESSBean extends EJB_TABLEAbstractBean {

    /** INSERT statement used in ejbCreate method (no sequence field, if any) */
    protected static final String SQL_INSERT = "INSERT INTO ADDRESS (FIRSTNAME,LASTNAME,STREET,CITY) VALUES (?,?,?,?)";
    /** SELECT statement used in ejbCreate method (excuted once to find the sequence value) */
    protected static final String SQL_SELECT_SEQUENCE = "SELECT MAX(ID) FROM ADDRESS";
    /** SELECT statement used in ejbLoad method */
    protected static final String SQL_SELECT = "SELECT FIRSTNAME,LASTNAME,STREET,CITY FROM ADDRESS WHERE ID=?";
    /** UPDATE statement used in ejbStore method */
    protected static final String SQL_UPDATE = "UPDATE ADDRESS SET FIRSTNAME=?,LASTNAME=?,STREET=?,CITY=? WHERE ID=?";
    /** DELETE statement used in ejbRemove method */
    protected static final String SQL_DELETE = "DELETE FROM ADDRESS WHERE ID=?";
    /** SELECT statement used in ejbFindByPrimaryKey method */
    protected static final String SQL_SELECT_FIND_BY_PRIMARY_KEY = "SELECT ID FROM ADDRESS WHERE ID=?";
    /** SELECT statement used in ejbFindAll method */
    protected static final String SQL_SELECT_FIND_ALL = "SELECT ID FROM ADDRESS";

    /** Full JNDI name of the DataSource */
    protected static final String DATA_SOURCE_JNDI_NAME = "java:/DefaultDS";

    /** Values object variable */
    protected ADDRESSValues _values = null;
    /** Autoincremented sequence value (shared by all EJB_ADDRESSBean objects) */
    protected static int _sequence = -1;

    /**
     * Default constructor (empty).
     */
    public EJB_ADDRESSBean() {}

    /**
     * Returns a values object that contains all ADDRESS fields
     *         (except primary key fields).
     * @return object values that contains all ADDRESS fields
     *         (except primary key fields).
     */
    public ADDRESSValues getAll() {
        return _values;
    }

    /**
     * Sets a values object that contains all ADDRESS
     *        fields for update (except primary key fields).
     * @param ADDRESSValues values object that contains all
     *        ADDRESS fields for update (except primary key fields).
     */
    public void setAll(ADDRESSValues values) {
        _values = values;
        _dirty = true;
    }

    /**
     * Creates a new ADDRESS row.
     * @param values a values object that contains values for the
     *        ADDRESS row to be created.
     * @return remote interface of the created entity bean (that maps the new row).
     * @throws javax.ejb.CreateException row count for INSERT != 1
     *                                   or unable to get the sequence value.
     * @throws javax.ejb.EJBException SQL error.
     */
    public EJB_ADDRESS_PK ejbCreate(ADDRESSValues values) throws CreateException {
        _values = values;
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_INSERT);
            pstmt.setString(1, _values.getFirstname());
            pstmt.setString(2, _values.getLastname());
            pstmt.setString(3, _values.getStreet());
            pstmt.setString(4, _values.getCity());
            if (pstmt.executeUpdate() != 1)
                throw new CreateException("Failed to create new ADDRESS row");
            if (_sequence == -1) {
                _sequence = getSequenceValue(conn, SQL_SELECT_SEQUENCE);
                if (_sequence == -1)
                    throw new CreateException("Failed to get current sequence value from ADDRESS");
            } else
                ++_sequence;
            return new EJB_ADDRESS_PK(_sequence);
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Post-creation method (empty).
     * @param values a values object that contains values for the newly
     *        created ADDRESS row.
     */
    public void ejbPostCreate(ADDRESSValues values) {}

    /**
     * Loads a ADDRESS row.
     * @throws javax.ejb.EJBException SQL error or empty ResultSet.
     */
    public void ejbLoad() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        EJB_ADDRESS_PK pk = (EJB_ADDRESS_PK)(_entityContext.getPrimaryKey());
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT);
            pstmt.setInt(1, pk.getId());
            rs = pstmt.executeQuery();
            if (!rs.next())
                throw new EJBException("Failed to load ADDRESS row with pk:" + pk);
            if (_values == null)
                _values = new ADDRESSValues(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
            else {
                _values.setFirstname(rs.getString(1));
                _values.setLastname(rs.getString(2));
                _values.setStreet(rs.getString(3));
                _values.setCity(rs.getString(4));
            }
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Updates a ADDRESS row.
     * @throws javax.ejb.EJBException SQL error or row count for UPDATE != 1.
     */
    public void ejbStore() {
        if (!_dirty)
            return;
        Connection conn = null;
        PreparedStatement pstmt = null;
        EJB_ADDRESS_PK pk = (EJB_ADDRESS_PK)(_entityContext.getPrimaryKey());
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_UPDATE);
            pstmt.setString(1, _values.getFirstname());
            pstmt.setString(2, _values.getLastname());
            pstmt.setString(3, _values.getStreet());
            pstmt.setString(4, _values.getCity());
            pstmt.setInt(5, pk.getId());
            if (pstmt.executeUpdate() != 1)
                throw new EJBException("Failed to update ADDRESS row with pk:" + pk);
            _dirty = false;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Deletes a ADDRESS row.
     * @throws javax.ejb.EJBException SQL error or row count for DELETE != 1.
     */
    public void ejbRemove() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        EJB_ADDRESS_PK pk = (EJB_ADDRESS_PK)(_entityContext.getPrimaryKey());
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_DELETE);
            pstmt.setInt(1, pk.getId());
            if (pstmt.executeUpdate() != 1)
                throw new EJBException("Failed to delete ADDRESS row with pk:" + pk);
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds a single row, provided a primary key.
     * @param pk the primary key that indentifies the row to retrieve.
     * @return the primary key (same as parameter pk).
     * @throws javax.ejb.ObjectNotFoundException no row with this primary key.
     * @throws javax.ejb.EJBException SQL error.
     */
    public EJB_ADDRESS_PK ejbFindByPrimaryKey(EJB_ADDRESS_PK pk) throws ObjectNotFoundException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_BY_PRIMARY_KEY);
            pstmt.setInt(1, pk.getId());
            rs = pstmt.executeQuery();
            if (!rs.next())
                throw new ObjectNotFoundException("No ADDRESS row with pk:" + pk);
            return pk;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds rows by id.
     * @param id value of the ID column.
     * @return primary keys collection of the founded rows.
     * @throws javax.ejb.EJBException SQL error.
     */
    public Collection ejbFindById(int id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_ALL + " WHERE ID=?");
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();
            Vector v = new Vector(128);
            while (rs.next())
                v.addElement(new EJB_ADDRESS_PK(rs.getInt(1)));
            return v;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds rows by firstname.
     * @param firstname value of the FIRSTNAME column.
     * @return primary keys collection of the founded rows.
     * @throws javax.ejb.EJBException SQL error.
     */
    public Collection ejbFindByFirstname(java.lang.String firstname) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_ALL + " WHERE FIRSTNAME=?");
            pstmt.setString(1, firstname);
            rs = pstmt.executeQuery();
            Vector v = new Vector(128);
            while (rs.next())
                v.addElement(new EJB_ADDRESS_PK(rs.getInt(1)));
            return v;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds rows by lastname.
     * @param lastname value of the LASTNAME column.
     * @return primary keys collection of the founded rows.
     * @throws javax.ejb.EJBException SQL error.
     */
    public Collection ejbFindByLastname(java.lang.String lastname) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_ALL + " WHERE LASTNAME=?");
            pstmt.setString(1, lastname);
            rs = pstmt.executeQuery();
            Vector v = new Vector(128);
            while (rs.next())
                v.addElement(new EJB_ADDRESS_PK(rs.getInt(1)));
            return v;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds rows by street.
     * @param street value of the STREET column.
     * @return primary keys collection of the founded rows.
     * @throws javax.ejb.EJBException SQL error.
     */
    public Collection ejbFindByStreet(java.lang.String street) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_ALL + " WHERE STREET=?");
            pstmt.setString(1, street);
            rs = pstmt.executeQuery();
            Vector v = new Vector(128);
            while (rs.next())
                v.addElement(new EJB_ADDRESS_PK(rs.getInt(1)));
            return v;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds rows by city.
     * @param city value of the CITY column.
     * @return primary keys collection of the founded rows.
     * @throws javax.ejb.EJBException SQL error.
     */
    public Collection ejbFindByCity(java.lang.String city) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_ALL + " WHERE CITY=?");
            pstmt.setString(1, city);
            rs = pstmt.executeQuery();
            Vector v = new Vector(128);
            while (rs.next())
                v.addElement(new EJB_ADDRESS_PK(rs.getInt(1)));
            return v;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Finds all rows in the ADDRESS table.
     * @return primary keys collection of the founded rows.
     * @throws javax.ejb.EJBException SQL error.
     */
    public Collection ejbFindAll() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            conn = _dataSource.getConnection();
            pstmt = conn.prepareStatement(SQL_SELECT_FIND_ALL);
            rs = pstmt.executeQuery();
            Vector v = new Vector(128);
            while (rs.next())
                v.addElement(new EJB_ADDRESS_PK(rs.getInt(1)));
            return v;
        } catch(SQLException e) {
            throw new EJBException(e);
        } finally {
            try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (pstmt != null) pstmt.close(); } catch (SQLException e) { e.printStackTrace(); }
            try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); }
        }
    }

    /**
     * Returns the full DataSource JNDI name.
     * @return the JNDI name.
     */
    protected String getDataSourceJNDIName() {
        return DATA_SOURCE_JNDI_NAME;
    }
}

EJB_ADDRESSBean.html (HTML view generated by ejen v.1.0.0).