⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enrollejb.java

📁 使用EJB开发的微型人力资源管理平台
💻 JAVA
字号:
package enroll.welfareejb;

import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;

public class EnrollEJB implements EntityBean
  {
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
   //这是一个基于Entity Bean的Bean Class,这个Entity Bean是基于
   //Bean-Managed Persistence的Entity Bean,用来对EnrollTBL表进行操作
   //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   public String employee_id;
   public ArrayList welfareItems;
   private Connection con;
   private String dbJndi = "java:comp/env/jdbc/EmployeeWelfareDB";
   private String dbId = "adm";
   private String dbPassword = "adm123";
   private EntityContext context;

   public void setEntityContext(EntityContext context)
     {
      this.context = context;
      try
        {
         getConnection();
        }
      catch(Exception e)
        {
         throw new EJBException("DB Connect failed: " + e.getMessage());
        }
     }

   public void unsetEntityContext()
     {
      try
        {
         con.close();
        }
      catch(SQLException e)
        {
         throw new EJBException("DB Close failed: " + e.getMessage());
        }
     }

   public String ejbCreate(String employee_id, ArrayList welfareItems) throws CreateException
     {
      if(welfareItems == null || welfareItems.size() == 0)
        {
         throw new CreateException("ejbCreate exception!");
        }
      this.employee_id = employee_id; 
      try
        {
         enroll(welfareItems);
        }
      catch(Exception e)
        {
         throw new EJBException("ejbCreate exception: "+e.getMessage());
        }
      this.welfareItems = welfareItems;
      return employee_id;
     }

   public void ejbPostCreate(String employee_id, ArrayList welfareItems)
     {}
     
   public String ejbFindByPrimaryKey(String employee_id) throws ObjectNotFoundException, FinderException
     {
      try
        {
         if(!selectByEmployeeId(employee_id))
           {
            throw new ObjectNotFoundException(employee_id + "not found!");
           }
        }
      catch(Exception e)
        {
         throw new FinderException("ejbFindByPrimaryKey exception!");
        }
      return employee_id;  
     }
  
   public Collection ejbFindAll() throws FinderException 
     {
      Collection result;
      try
        {
         result = selectAll();
        }
      catch(Exception e)
        {
         throw new FinderException(e.getMessage());
        }
      if(result.isEmpty())
        {
         throw new ObjectNotFoundException("No result found!");
        }
      else
        {
         return result;
        }
     }
      
   public ArrayList getWelfareItems()
     {
      return welfareItems;
     }


   public String getEmployee_id()
     {
      return employee_id;
     }
    
   public void replaceWelfareItems(ArrayList welfareItems)
     {
      if(welfareItems == null || welfareItems.size() == 0)
        {    
         throw new EJBException("replaceWelfareItems exception!");
        }
      try
        {
         unenroll();
         enroll(welfareItems);
        }
      catch(Exception e)
        {
         throw new EJBException(e.getMessage());
        }
      this.welfareItems=welfareItems;
     }

   public void ejbLoad()
     {
      try
        {
         loadWelfares();
        }
      catch(Exception e)
        {
         throw new EJBException(e.getMessage());
        }
     }

   public void ejbStore()
     {}
  
   public void ejbActivate()
     {
      employee_id = (String)context.getPrimaryKey();
     }

   public void ejbPassivate()
     {
      employee_id = null;
     }	
   
   public void ejbRemove()
     {
      try
        {
         unenroll();
        }
      catch(Exception e)
        {
         throw new EJBException(e.getMessage());
        }
     } 
 
   private void getConnection() throws NamingException, SQLException
     {
      //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
      // 取得数据库的连接
      //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

      InitialContext ic = new InitialContext();
      DataSource ds = (DataSource) ic.lookup(dbJndi);
      con = ds.getConnection(dbId, dbPassword);
     }

   private void enroll(ArrayList welfareItems) throws SQLException
     {
      String insertStatement = "insert into EnrollTBL values(? ,?)";
      PreparedStatement ps = con.prepareStatement(insertStatement);

      try
        { 
         //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
         // 依次将所有的福利项目插入EnrollTBL表
         //- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

         ps.setString(1, this.employee_id);  
         for(int i = 0; i < welfareItems.size(); i++)
           {
            String welfare_id = (String)welfareItems.get(i);
            ps.setString(2, welfare_id);
            ps.executeUpdate();
           }
        }
      finally
        {
         ps.close();
        }      
     }

   private boolean selectByEmployeeId(String employee_id) throws SQLException
     {
      String sqlStatement = "select distinct employee_id from EnrollTBL " + " where employee_id = ?";
      PreparedStatement ps = con.prepareStatement(sqlStatement);

      try
        {  
         ps.setString(1, employee_id);
         ResultSet rs = ps.executeQuery();
         if(rs.next())
           {
            return true;
           }
         return false;
        }
      finally
        {
         ps.close();
        }
     }
   
   private void unenroll() throws SQLException
     {
      String deleteStatement = "delete from EnrollTBL " + " where employee_id=?";
      PreparedStatement ps = con.prepareStatement(deleteStatement);

      try
        {
         ps.setString(1, employee_id);
         ps.executeUpdate();
        }
      finally
        {  
         ps.close();
        }  
     }

   private void loadWelfares() throws SQLException
     {
      String selectStatement = "select welfare_id " + " from EnrollTBL where employee_id=? ";
      PreparedStatement ps = con.prepareStatement(selectStatement);
      
      try
        {        
         ps.setString(1, employee_id);
         ResultSet rs = ps.executeQuery();
         welfareItems = new ArrayList();
   
         while(rs.next())
           {
            String welfare_id = rs.getString(1);
            welfareItems.add(welfare_id);
           }
        }
      finally
        {  
         ps.close();
        }  
     }
  
   private Collection selectAll() throws SQLException
     {
      String sqlStatement = "select distinct employee_id " + " from EnrollTBL order by employee_id";
      PreparedStatement ps = con.prepareStatement(sqlStatement);
    
      try
        {
         ResultSet rs = ps.executeQuery();
         ArrayList al = new ArrayList();
         
         while(rs.next())
           {
            String id = rs.getString(1);
            al.add(id);
           }
         
         return al;
        }
      finally
        {  
         ps.close();
        }      
     }
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -