StudentEJB.java
资源名称:某公司的java培训教材 [点击查看]
上传用户:dinglihq
上传日期:2013-02-04
资源大小:99958k
文件大小:7k
源码类别:
Java编程
开发平台:
Java
- package bible.ejb.entity.bmp;
- import javax.ejb.*;
- import java.sql.*;
- import javax.sql.DataSource;
- import javax.naming.InitialContext;
- import java.util.Collection;
- import java.util.Vector;
- public class StudentEJB implements EntityBean
- {
- private EntityContext context;
- private static final String DATASOURCE
- = "java:comp/env/jdbc/connectionPool";
- public String id;
- public String firstName;
- public String lastName;
- public String eMailAddr;
- public String phone;
- public String ssNum;
- public void setEntityContext(EntityContext ec) {
- context = ec;
- }
- public void unsetEntityContext() {
- this.context = null;
- }
- public StudentPK ejbCreate(StudentVO student)
- throws CreateException
- {
- this.id = student.getId();
- setStudentData(student);
- Connection con = null;
- PreparedStatement ps = null;
- StudentPK key = null;
- try {
- con = getConnection();
- ps = con.prepareStatement("insert into STUDENT (ID,FIRST_NAME, LAST_NAME, EMAIL_ADDR, PHONE, SSNUM) values (?, ?, ?, ?, ?, ?)");
- ps.setString(1, id);
- ps.setString(2, firstName);
- ps.setString(3, lastName);
- ps.setString(4, eMailAddr);
- ps.setString(5, phone);
- ps.setString(6, ssNum);
- int ret = ps.executeUpdate();
- if (ret != 1) {
- throw new CreateException("Create failed");
- }
- key = new StudentPK();
- key.id = id;
- return key;
- }
- catch (Exception e) {
- throw new CreateException(e.getMessage());
- }
- finally {
- cleanup(con, ps);
- }
- }
- public void ejbPostCreate(StudentVO student) {
- }
- public void ejbLoad()
- {
- StudentPK key = (StudentPK)context.getPrimaryKey();
- try {
- readData(key);
- }
- catch(Exception e) {
- throw new EJBException(e.getMessage());
- }
- }
- public void ejbStore()
- {
- Connection con = null;
- PreparedStatement ps = null;
- try {
- con = getConnection();
- StudentPK key =
- (StudentPK)context.getPrimaryKey();
- ps = con.prepareStatement("update STUDENT set FIRST_NAME = ?,LAST_NAME = ?,EMAIL_ADDR = ?,PHONE = ?,SSNUM = ? where ID = ?");
- ps.setString(1, firstName);
- ps.setString(2, lastName);
- ps.setString(3, eMailAddr);
- ps.setString(4, phone);
- ps.setString(5, ssNum);
- ps.setString(6, key.id);
- int ret = ps.executeUpdate();
- if (ret == 0) {
- throw new EJBException("ejbStore failed");
- }
- }
- catch (Exception e) {
- throw new EJBException(e.getMessage());
- }
- finally {
- cleanup(con, ps);
- }
- }
- public void ejbRemove() throws RemoveException
- {
- Connection con = null;
- PreparedStatement ps = null;
- try {
- con = getConnection();
- StudentPK key =
- (StudentPK) context.getPrimaryKey();
- ps = con.prepareStatement("delete from STUDENT where ID=?");
- ps.setString(1, key.id);
- int result = ps.executeUpdate();
- if (result == 0) {
- throw new RemoveException("Remove failed");
- }
- }
- catch (Exception e) {
- throw new RemoveException(e.getMessage());
- }
- finally {
- cleanup(con, ps);
- }
- }
- public void ejbActivate() {
- }
- public void ejbPassivate() {
- }
- public StudentPK ejbFindByPrimaryKey(StudentPK key)
- throws FinderException
- {
- try {
- readData(key);
- return key;
- }
- catch (Exception e) {
- throw new FinderException(e.getMessage());
- }
- }
- public StudentPK ejbFindBySSNum(String ssNum)
- throws FinderException
- {
- Connection con = null;
- PreparedStatement ps = null;
- try {
- con = getConnection();
- ps = con.prepareStatement("select ID, FIRST_NAME,LAST_NAME,EMAIL_ADDR,PHONE,SSNUM from STUDENT where SSNUM = ?");
- ps.setString(1, ssNum);
- ResultSet rs = ps.executeQuery();
- if (!rs.next()) {
- throw new FinderException("Record not found");
- }
- else {
- loadAttributes(rs);
- }
- rs.close();
- StudentPK key = new StudentPK();
- key.id = id;
- return key;
- }
- catch (Exception e) {
- throw new EJBException(e.getMessage());
- }
- finally {
- cleanup(con, ps);
- }
- }
- public Collection ejbFindByLastName(String lastName)
- throws FinderException
- {
- Connection con = null;
- PreparedStatement ps = null;
- try {
- con = getConnection();
- ps = con.prepareStatement("select ID from STUDENT where LAST_NAME = ?");
- ps.setString(1, lastName);
- ResultSet rs = ps.executeQuery();
- Vector students = new Vector();
- StudentPK key = null;
- while (rs.next()) {
- key = new StudentPK();
- key.id = rs.getString(1);
- students.addElement(key);
- }
- rs.close();
- return students;
- }
- catch (Exception e) {
- throw new FinderException(e.getMessage());
- }
- finally {
- cleanup(con, ps);
- }
- }
- private void loadAttributes(ResultSet rs)
- throws SQLException
- {
- id = rs.getString(1);
- firstName = rs.getString(2);
- lastName = rs.getString(3);
- eMailAddr = rs.getString(4);
- phone = rs.getString(5);
- ssNum = rs.getString(6);
- }
- private void cleanup(Connection con, PreparedStatement ps)
- {
- try {
- if (ps != null) {
- ps.close();
- }
- if (con != null) {
- con.close();
- }
- }
- catch (Exception e) {
- throw new EJBException (e);
- }
- }
- public StudentVO getStudentData()
- {
- StudentVO student = new StudentVO();
- student.setFirstName(firstName);
- student.setLastName(lastName);
- student.setPhone(phone);
- student.setEMailAddr(eMailAddr);
- student.setSSNum(ssNum);
- return student;
- }
- public void setStudentData(StudentVO student)
- {
- firstName = student.getFirstName();
- lastName = student.getLastName();
- eMailAddr = student.getEMailAddr();
- phone = student.getPhone();
- ssNum = student.getSSNum();
- }
- public String getFirstName() {
- return(firstName);
- }
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
- public String getEMailAddr() {
- return eMailAddr;
- }
- public void setEMailAddr(String eMailAddr) {
- this.eMailAddr = eMailAddr;
- }
- public String getPhone() {
- return phone;
- }
- public void setPhone(String phone) {
- this.phone = phone;
- }
- public String getSSNum() {
- return ssNum;
- }
- public void setSSNum(String ssNum) {
- this.ssNum = ssNum;
- }
- private Connection getConnection() throws EJBException
- {
- try {
- InitialContext ic = new InitialContext();
- DataSource ds = (DataSource)ic.lookup(DATASOURCE);
- return ds.getConnection();
- }
- catch (Exception e) {
- throw new EJBException(e.getMessage());
- }
- }
- private void readData(StudentPK key) throws
- FinderException, EJBException
- {
- Connection con = null;
- PreparedStatement ps = null;
- try {
- con = getConnection();
- ps = con.prepareStatement("select ID, FIRST_NAME,LAST_NAME,EMAIL_ADDR,PHONE,SSNUM from STUDENT where ID = ?");
- ps.setString(1, key.id);
- ResultSet rs = ps.executeQuery();
- if (!rs.next()) {
- throw new FinderException("Record not found");
- }
- else {
- loadAttributes(rs);
- }
- rs.close();
- }
- catch (Exception e){
- throw new EJBException(e.getMessage());
- }
- finally {
- cleanup(con, ps);
- }
- }
- }