A.3 PayrollEJB Session Bean Class

Here is the implementation of the PayrollEJB session bean class used in the session bean Benefits Enrollment application.

Code Example A.3 PayrollEJB Session Bean Source Code
 package com.star.payroll; import javax.ejb.*; import javax.naming.*; import java.sql.*; import javax.sql.*; // Payroll is a stateless session bean that provides // access to the payroll system. // public class PayrollBean implements SessionBean {     private DataSource ds;     public void setBenefitsDeduction(int emplNumber,            double deduction) throws PayrollException     {         try {             Connection con = getConnection();             PreparedStatement pstmt = con.prepareStatement(                 "UPDATE Paychecks SET " +                 "pay_ded_benefits = ? " +                 "WHERE pay_empl = ?"             );             pstmt.setDouble(1, deduction);             pstmt.setInt(2, emplNumber);             if (pstmt.executeUpdate() == 0) {                 con.close();                 throw new PayrollException(                     PayrollException.INVAL_EMPL_NUMBER);             }             con.close();         } catch (Exception ex) {             throw new EJBException(ex);         }     }     public double getBenefitsDeduction(int emplNumber)         throws PayrollException     {         try {             Connection con = getConnection();             Statement stmt = con.createStatement();             PreparedStatement pstmt = con.prepareStatement(                 "SELECT pay_ded_benefits " +                 "FROM Paychecks " +                 "WHERE pay_emp = ?"             );             pstmt.setInt(1, emplNumber);             ResultSet rs = pstmt.executeQuery();             if (rs.next()) {                 double deduction = rs.getDouble(1);                 con.close();                 return deduction;             } else {                 con.close();                 throw new PayrollException(                     PayrollException.INVAL_EMPL_NUMBER);             }         } catch (SQLException ex) {             throw new EJBException(ex);         }     }     public double getSalary(int emplNumber)         throws PayrollException     {         try {             Connection con = getConnection();             Statement stmt = con.createStatement();             PreparedStatement pstmt = con.prepareStatement(                 "SELECT pay_salary " +                 "FROM Paychecks " +                 "WHERE pay_emp = ?"             );             pstmt.setInt(1, emplNumber);             ResultSet rs = pstmt.executeQuery();             if (rs.next()) {                 double salary = rs.getDouble(1);                 con.close();                 return salary;             } else {                 con.close();                 throw new PayrollException(                     PayrollException.INVAL_EMPL_NUMBER);             }         } catch (SQLException ex) {             throw new EJBException(ex);         }     }     public void setSalary(int emplNumber, double salary)         throws PayrollException     {         try {             Connection con = getConnection();             PreparedStatement pstmt = con.prepareStatement(                 "UPDATE Paychecks SET " +                 "pay_salary = ? " +                 "WHERE pay_empl = ?"             );             pstmt.setDouble(1, salary);             pstmt.setInt(2, emplNumber);             if (pstmt.executeUpdate() == 0) {                 con.close();                 throw new PayrollException(                     PayrollException.INVAL_EMPL_NUMBER);             }             con.close();         } catch (Exception ex) {             throw new EJBException(ex);         }     }     public void setSessionContext(SessionContext sc) {         readEnvironment();     }     public void ejbCreate() {}     public void ejbRemove() {}     public void ejbPassivate() { /* never called */ }     public void ejbActivate() { /* never called */ }     private Connection getConnection() {         try {             return ds.getConnection();         } catch (Exception ex) {             throw new EJBException(ex);         }     }     private void readEnvironment() {         try {             Context ictx = new InitialContext();             ds = (DataSource)ictx.lookup(                 "java:comp/env/jdbc/PayrollDB");         } catch (Exception ex) {             throw new EJBException(ex);         }     } } 


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