📄 enrollsessionejb.java
字号:
package enroll.welfareejb;
import javax.ejb.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.naming.*;
import java.rmi.RemoteException;
import javax.rmi.PortableRemoteObject;
public class EnrollSessionEJB implements SessionBean
{
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
//这是一个基于Session Bean的Bean Class,这个Session Bean是基于
//Stateful的Session Bean,用来对特定员工的登记过程进行操作
//- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
private Connection con;
private String dbJndi = "java:comp/env/jdbc/EmployeeWelfareDB";
private String dbId = "adm";
private String dbPassword = "adm123";
public EmployeeHome sHome;
public EnrollHome eHome;
public String employee_id;
public String name;
public EnrollSessionEJB()
{}
public void setSessionContext(SessionContext sc)
{
try
{
Context initial = new InitialContext();
Object objref = initial.lookup("ejb/EmployeeEntityBean");
sHome = (EmployeeHome)PortableRemoteObject.narrow(objref, EmployeeHome.class);
objref = initial.lookup("ejb/EnrollEntityBean");
eHome = (EnrollHome)PortableRemoteObject.narrow(objref, EnrollHome.class);
}
catch(Exception e)
{
throw new EJBException("DB connect failed: " + e.getMessage());
}
}
public void ejbCreate(String employee_id) throws CreateException
{
try
{
Employee employee = sHome.findByPrimaryKey(employee_id);
name = employee.getName();
}
catch (ObjectNotFoundException e)
{
throw new CreateException("Employee: " + employee_id + " not found in EmployeeTBL!");
}
catch(Exception e)
{
throw new EJBException(e.getMessage());
}
this.employee_id = employee_id;
}
public String getEmployeeName()
{
// 获取员工的姓名
return name;
}
public void enroll(ArrayList welfareItems)
{
Enroll enroll = null;
try
{
enroll = eHome.findByPrimaryKey(employee_id);
}
catch (Exception e)
{}
try
{
if(enroll != null)
{
enroll.replaceWelfareItems(welfareItems);
}
else
{
eHome.create(employee_id, welfareItems);
}
}
catch (Exception e)
{
throw new EJBException(e.getMessage());
}
}
public void unenroll()
{
try
{
Enroll enroll = eHome.findByPrimaryKey(employee_id);
enroll.remove();
}
catch(Exception e)
{
throw new EJBException(e.getMessage());
}
}
public void deleteEmployee() throws FinderException
{
try
{
Enroll enroll = eHome.findByPrimaryKey(employee_id);
Employee employee = sHome.findByPrimaryKey(employee_id);
enroll.remove();
employee.remove();
}
catch (Exception e)
{
throw new EJBException(e.getMessage());
}
}
public void deleteWelfare(String welfare_id)
{
PreparedStatement ps = null;
try
{
getConnection();
String deleteStatement = " delete from EnrollTBL " + " where employee_id=? and welfare_id=? ";
ps = con.prepareStatement(deleteStatement);
ps.setString(1, employee_id);
ps.setString(2, welfare_id);
ps.executeUpdate();
}
catch (Exception e)
{
throw new EJBException(e.getMessage());
}
finally
{
try
{
ps.close();
con.close();
}
catch(Exception e)
{
throw new EJBException(e.getMessage());
}
}
}
public void ejbActivate()
{}
public void ejbPassivate()
{}
public void ejbRemove()
{
sHome = null;
eHome = null;
}
private void getConnection() throws NamingException, SQLException
{
InitialContext ic = new InitialContext();
DataSource ds = (DataSource) ic.lookup(dbJndi);
con = ds.getConnection(dbId, dbPassword);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -