A.7 EmployeeBeanBMP Class

A.7 EmployeeBeanBMP Class

This is the source code for the EmployeeBeanBMP class.

Code Example A.7 EmployeeBeanBMP Source Code
 package com.star.benefits; import java.util.Date; import java.sql.*; import javax.sql.*; import javax.naming.*; import javax.ejb.*; import com.wombat.benefits.EmployeeBean; public class EmployeeBeanBMP extends EmployeeBean {     private DataSource dataSource;     private Integer employeeNumber;     private String firstName;     private String lastName;     private Date birthDate;     // Implementations of CMP field get/set methods     public Integer getEmployeeNumber() {     return employeeNumber;     }     public void setEmployeeNumber(Integer n) {     employeeNumber = n;     }     public String getFirstName() {     return firstName;     }     public void setFirstName(String s) {     firstName = s;     }     public String getLastName() {     return lastName;     }     public void setLastName(String s) {     lastName = s;     }     public Date getBirthDate() {     return birthDate;     }     public void setBirthDate(Date d) {     birthDate = d;     }     // EntityBean life-cycle methods     public void setEntityContext(EntityContext c)     {         super.setEntityContext(c);     String dataSourceName = "java:comp/env/jdbc/EmployeeDatabase";         try {             Context ctx = new InitialContext();             dataSource = (DataSource)ctx.lookup(dataSourceName);         } catch ( Exception ex ) {             throw new EJBException("Unable to look up dataSource "                   + dataSourceName);         }     }     public void unsetEntityContext()     {     dataSource = null;     super.unsetEntityContext();     }     public Integer ejbCreate(int emplNumber, String fname,                String lname, Date birthDate)                throws CreateException     {     // This sets all the CMP fields.     super.ejbCreate(emplNumber, fname, lname, birthDate);     // Check if the primary key exists.     if ( primaryKeyExists(emplNumber) ) {         throw new DuplicateKeyException(                "Employee number " + emplNumber +                " already exists in database");     }     // C.reate a row for this bean instance     createRow();     // Return the primary key.     return new Integer(emplNumber);     }     public void ejbPostCreate(int emplNumber, String fname,                String lname, Date birthDate)                throws CreateException     {}     public Integer ejbFindByPrimaryKey(Integer primaryKey)         throws FinderException     {         // Try to load the row for this primary key.         if ( !primaryKeyExists(primaryKey.intValue()) ) {             throw new ObjectNotFoundException(                   "Primary key " + primaryKey                    + " not found");     }         return primaryKey;     }     public void ejbRemove() throws RemoveException     {     super.ejbRemove();         // Remove the row for this primary key.         removeRow();     // Clear all CMP fields.     employeeNumber = null;     firstName = null;     lastName = null;     birthDate = null;     }     public void ejbLoad()     {         try {             loadRow();         } catch ( Exception ex ) {             throw new NoSuchEntityException(                   "Exception caught in ejbLoad: " + ex);         }     super.ejbLoad();     }     public void ejbStore()     {     super.ejbStore();         try {             storeRow();         } catch ( Exception ex ) {             throw new EJBException("Exception caught in ejbStore ",                   ex);         }     }     public void ejbActivate()     {         employeeNumber = (Integer)entityContext.getPrimaryKey();     }     public void ejbPassivate()     {     // Clear all CMP fields.     employeeNumber = null;     firstName = null;     lastName = null;     birthDate = null;     } /***************************************************************/ /* Below is all the database access code **********************************/ /     private void createRow()     {         // Create row for this accountId.     Connection con=null;     PreparedStatement stmt=null;     int resultCount;     try {         con = dataSource.getConnection();         String query = "INSERT INTO employees            (empl_id, empl_first_name, empl_last_name,            empl_birth_date) VALUES (?, ?, ?, ?)";         stmt = con.prepareStatement(query);         stmt.setInt(1, employeeNumber.intValue());         stmt.setString(2, firstName);         stmt.setString(3, lastName);         stmt.setDate(4, new java.sql.Date(birthDate.getTime()));         resultCount = stmt.executeUpdate();     } catch ( Exception ex ) {         throw new EJBException(ex);     } finally {         try {         if ( con != null ) con.close();         if ( stmt != null ) stmt.close();         } catch ( Exception ex ) {}     }         if ( resultCount != 1 )             throw new EJBException("Unable to insert row");     }     private void removeRow()     {     Connection con=null;     Statement stmt=null;     int resultCount;     try {         con = dataSource.getConnection();         stmt = con.createStatement();         String query =         "DELETE FROM employees WHERE empl_id = " + employeeNumber;         resultCount = stmt.executeUpdate(query);     } catch ( Exception ex ) {         throw new EJBException(ex);     } finally {         try {         if ( con != null ) con.close();         if ( stmt != null ) stmt.close();         } catch ( Exception ex ) {}     }         if ( resultCount != 1 )             throw new EJBException("Unable to delete row");     }     private boolean primaryKeyExists(int emplNum)     {     Connection con=null;     Statement stmt=null;     try {         con = dataSource.getConnection();         stmt = con.createStatement();         String query = "SELECT empl_id"            + " FROM employees WHERE empl_id = " + emplNum;         ResultSet result = stmt.executeQuery(query);         if ( !result.next() )         return false;         return true;     } catch ( Exception ex ) {         throw new EJBException(ex);     } finally {         try {         if ( con != null ) con.close();         if ( stmt != null ) stmt.close();         } catch ( Exception ex ) {}     }      }     private void loadRow()     {     Connection con=null;     Statement stmt=null;     try {         con = dataSource.getConnection();         stmt = con.createStatement();         String query = "SELECT empl_first_name, empl_last_name,                empl_birth_date"            + " FROM employees WHERE empl_id = " + employeeNumber;         ResultSet result = stmt.executeQuery(query);         if ( !result.next() )         throw new NoSuchEntityException(                "No database row for primary key: " + employeeNumber);         firstName = result.getString(1);         lastName = result.getString(2);         birthDate = result.getDate(3);     } catch ( Exception ex ) {         throw new EJBException(ex);     } finally {         try {         if ( con != null ) con.close();         if ( stmt != null ) stmt.close();         } catch ( Exception ex ) {}     }      }     private void storeRow()     {     Connection con=null;         PreparedStatement stmt=null;     int resultCount;     try {         con = dataSource.getConnection();         String query =  "UPDATE employees SET empl_first_name = ?,            empl_last_name = ?, empl_birth_date = ?            WHERE empl_id = ?";         stmt = con.prepareStatement(query);         stmt.setString(1, firstName);         stmt.setString(2, lastName);         stmt.setDate(3, new java.sql.Date(birthDate.getTime()));         stmt.setInt(4, employeeNumber.intValue());         resultCount = stmt.executeUpdate();     } catch ( Exception ex ) {         throw new EJBException(ex);     } finally {         try {         if ( con != null ) con.close();         if ( stmt != null ) stmt.close();         } catch ( Exception ex ) {}     }         if ( resultCount != 1 )             throw new EJBException("Unable to store row");     } } 


Applying Enterprise Javabeans
Applying Enterprise JavaBeans(TM): Component-Based Development for the J2EE(TM) Platform
ISBN: 0201702673
EAN: 2147483647
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net