📄 employeebean.java
字号:
package com.entity.bmp;import javax.ejb.CreateException;import javax.ejb.EntityBean;import javax.ejb.EntityContext;import javax.ejb.FinderException;import javax.ejb.*;import java.sql.*;import java.math.BigDecimal;//import javax.sql.*;import javax.naming.*;import java.math.*;import java.sql.*;/** * This is the EmployeeBean interface. * * @author: Siddhartha P. Chandurkar * @version $ID: $ * */public class EmployeeBean implements EntityBean{ //ATTRIBUTES private transient EntityContext context; public Integer id; public Integer deptId; public String firstName; public String lastName; public String sex; public Date birthDate; public Double salary; public String department; //CONSTRUCTORS public EmployeeBean(){ } //METHODS //Get Methods public Integer getId() { return id; } public Integer getDeptId() { return deptId; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } public String getSex() { return sex; } public Date getBirthDate() { return birthDate; } public Double getSalary() { return salary; } public String getDepartment() { return department; } //Set methods public void setId(Integer _id) { id = _id; } public void setDeptId(Integer _deptId) { deptId = _deptId; } public void setFirstName(String _firstName) { firstName = _firstName; } public void setLastName(String _lastName) { lastName = _lastName; } public void setSex(String _sex) { sex = _sex; } public void setBirthDate(Date _birthDate) { birthDate = _birthDate; } public void setSalary(Double _salary) { salary = _salary; } public void setDepartment(String _department) { department = _department; } /** * */ public void bar()throws MyException, MyOtherException{ } /** * */ public void foo(){ } /** * */ public void setEntityContext(EntityContext context){ this.context = context; } /** * */ public void unsetEntityContext(){ context = null; } /** * */ public void ejbActivate() { } /** * */ public void ejbPassivate(){ } /** * */ public EmployeePK ejbCreate(Integer _id, Integer _deptId, String _firstName, String _lastName, String _sex, Date _birthDate, Double _salary, String _department){ id = _id; deptId = _deptId; firstName = _firstName; lastName = _lastName; sex = _sex; birthDate = _birthDate; salary = _salary; department = _department; PreparedStatement pStmt = null; Connection conn = null; try{ conn = getConnection(); String query = "insert into Employee (id, deptId, firstName, lastName, sex, birthDate, salary, department) values (?, ?, ?, ?, ?, ?, ?, ?)"; pStmt = conn.prepareStatement(query); pStmt.setInt(1,id.intValue()); pStmt.setInt(2,deptId.intValue()); pStmt.setString(3,firstName); pStmt.setString(4,lastName); pStmt.setString(5,sex); pStmt.setDate(6,birthDate); pStmt.setDouble(7,salary.doubleValue()); pStmt.setString(8,department); if(pStmt.executeUpdate() != 1){ throw new CreateException("Failed to add Employee to database"); } return new EmployeePK(deptId ,id); }catch(SQLException ex){ throw new EJBException(ex); }catch(Exception ex){ throw new EJBException(ex); }finally{ try{ if(pStmt != null) pStmt.close();} catch(Exception e){} try{ if(conn != null) conn.close();} catch(Exception e){} } } /** * */ public void ejbPostCreate(Integer _id, Integer _deptId, String _firstName, String _lastName, String _sex, Date _birthDate, Double _salary, String _department){ //Do Something Usefull } /** * */ public void ejbLoad() { EmployeePK pk = (EmployeePK )context.getPrimaryKey(); java.lang.Integer deptId = pk.getDeptId(); java.lang.Integer id = pk.getId(); PreparedStatement pStmt = null; Connection conn = null; try{ conn = getConnection(); String query = "select ID,DEPTID,FIRSTNAME,LASTNAME,SEX,BIRTHDATE,SALARY,DEPARTMENT from Employee where DEPTID = ? AND ID = ? "; pStmt = conn.prepareStatement(query); pStmt.setInt(1, deptId.intValue()); pStmt.setInt(2, id.intValue()); ResultSet rs = pStmt.executeQuery(); rs.next(); setId(new Integer(rs.getInt("ID"))); setDeptId(new Integer(rs.getInt("DEPTID"))); setFirstName(rs.getString("FIRSTNAME")); setLastName(rs.getString("LASTNAME")); setSex(rs.getString("SEX")); setBirthDate(rs.getDate("BIRTHDATE")); setSalary(new Double(rs.getDouble("SALARY"))); setDepartment(rs.getString("DEPARTMENT")); }catch(SQLException ex){ throw new EJBException(ex); }catch(Exception ex){ throw new EJBException(ex); }finally{ try{ if(pStmt != null) pStmt.close();} catch(Exception e){} try{ if(conn != null) conn.close();} catch(Exception e){} } } /** * */ public void ejbStore() { PreparedStatement pStmt = null; Connection conn = null; try{ conn = getConnection(); String query = "update Employee set FIRSTNAME = ? , LASTNAME = ? , SEX = ? , BIRTHDATE = ? , SALARY = ? , DEPARTMENT = ? where ID = ? AND DEPTID = ? "; pStmt = conn.prepareStatement(query); pStmt.setString(1,firstName); pStmt.setString(2,lastName); pStmt.setString(3,sex); pStmt.setDate(4,birthDate); pStmt.setDouble(5,salary.doubleValue()); pStmt.setString(6,department); pStmt.setInt(7,id.intValue()); pStmt.setInt(8,deptId.intValue()); }catch(SQLException ex){ throw new EJBException(ex); }catch(Exception ex){ throw new EJBException(ex); }finally{ try{ if(pStmt != null) pStmt.close();} catch(Exception e){} try{ if(conn != null) conn.close();} catch(Exception e){} } } /** * */ public void ejbRemove() { EmployeePK pk = (EmployeePK )context.getPrimaryKey(); java.lang.Integer deptId = pk.getDeptId(); java.lang.Integer id = pk.getId(); PreparedStatement pStmt = null; Connection conn = null; try{ conn = getConnection(); String query = "delete from Employee where DEPTID = ? AND ID = ? "; pStmt = conn.prepareStatement(query); pStmt.setInt(1, deptId.intValue()); pStmt.setInt(2, id.intValue()); if(pStmt.executeUpdate() == 0){ throw new RemoveException("Employee's primary key(s) deptId AND id failed to remove from the database" ); } }catch(SQLException ex){ throw new EJBException(ex); }catch(Exception ex){ throw new EJBException(ex); }finally{ try{ if(pStmt != null) pStmt.close();} catch(Exception e){} try{ if(conn != null) conn.close();} catch(Exception e){} } } public EmployeePK ejbFindByPrimaryKey(EmployeePK pk) throws FinderException{ java.lang.Integer deptId = pk.getDeptId(); java.lang.Integer id = pk.getId(); PreparedStatement pStmt = null; Connection conn = null; try{ conn = getConnection(); String query = "select ID,DEPTID,FIRSTNAME,LASTNAME,SEX,BIRTHDATE,SALARY,DEPARTMENT from Employee where DEPTID = ? AND ID = ? "; pStmt = conn.prepareStatement(query); pStmt.setInt(1, deptId.intValue()); pStmt.setInt(2, id.intValue()); ResultSet rs = pStmt.executeQuery(); return pk; }catch(Exception ex){ throw new FinderException(); }finally{ try{ if(pStmt != null) pStmt.close();} catch(Exception e){} try{ if(conn != null) conn.close();} catch(Exception e){} } } /** * Gets The DataSource Connection */ public Connection getConnection()throws Exception{ try{ Context ctx = new InitialContext(); javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup("java:/DefaultDS"); return ds.getConnection(); }catch(Exception e){ e.printStackTrace(); throw e; } } /** * Write your manual code between these tags. They will be retained if code is * regenerated. * IMPORTANT !!! Please do not remove these tags even if you dont add any manual code */ //<manual-code> //</manual-code> }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -