📄 doctorejb.java
字号:
package wlsunleashed.ejb.entity.bmp;
import javax.ejb.*;
import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
public class DoctorEJB implements EntityBean {
private EntityContext ctx;
private static final String DATASOURCE = "java:comp/env/TxDataSource";
private Integer id;
private String firstName;
private String lastName;
private String specialty;
public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
}
public void unsetEntityContext() {
this.ctx = null;
}
public void ejbActivate() {}
public void ejbPassivate() {}
public Integer ejbCreate(DoctorVO vo) throws CreateException
{
System.out.println("DoctorEJB ejbCreate DoctorVO = " + vo.toString());
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
id = vo.getId();
firstName = vo.getFirstName();
lastName = vo.getLastName();
specialty = vo.getSpecialty();
ps = conn.prepareStatement("INSERT INTO Doctor (ID, FIRSTNAME, LASTNAME, SPECIALTY) VALUES (?,?,?,?)");
ps.setInt(1, id.intValue());
ps.setString(2, firstName);
ps.setString(3, lastName);
ps.setString(4, specialty);
int numrows = ps.executeUpdate();
if (numrows != 1)
throw new CreateException("Cannot create Doctor numrows = " + numrows);
ps.close();
return id;
} catch (Exception e) {
throw new CreateException("Cannot create Doctor " + e.getMessage());
} finally {
closeResources(ps, conn);
}
}
public void ejbPostCreate(DoctorVO vo)
{
}
public void ejbLoad() {
Integer id = (Integer)ctx.getPrimaryKey();
try {
readFromDb(id.intValue());
} catch (Exception e) {
throw new EJBException(e);
}
}
public void ejbStore() {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement("UPDATE DOCTOR SET FIRSTNAME = ?, LASTNAME = ?, SPECIALTY = ? WHERE ID = ?");
ps.setString(1, firstName);
ps.setString(2, lastName);
ps.setString(3, specialty);
Integer id = (Integer)ctx.getPrimaryKey();
ps.setInt(4, id.intValue());
int numrows = ps.executeUpdate();
if (numrows != 1)
throw new EJBException("Cannot update Doctor, id = " + id.toString());
} catch (Exception e) {
throw new EJBException(e);
} finally {
closeResources(ps, conn);
}
}
public void ejbRemove() {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement("DELETE FROM Doctor WHERE ID = ?");
Integer id = (Integer)ctx.getPrimaryKey();
ps.setInt(1, id.intValue());
int numrows = ps.executeUpdate();
if (numrows != 1)
throw new EJBException("Cannot delete Doctor, id = " + id.toString());
} catch (Exception e) {
throw new EJBException(e);
} finally {
closeResources(ps, conn);
}
}
public Integer ejbFindByPrimaryKey(Integer id) throws FinderException {
try {
readFromDb(id.intValue());
return id;
} catch(Exception e) {
throw new FinderException(e.getMessage());
}
}
public Collection ejbFindBySpecialty(String specialty) throws FinderException {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement("SELECT id FROM Doctor WHERE UPPER(specialty) LIKE UPPER(?)");
ps.setString(1, specialty);
Vector doctors = new Vector();
ResultSet rset = ps.executeQuery();
while(rset.next()) {
Integer id = new Integer(rset.getInt(1));
doctors.addElement(id);
}
rset.close();
return doctors;
} catch (Exception e) {
throw new FinderException(e.getMessage());
} finally {
closeResources(ps, conn);
}
}
public Collection ejbFindByName(String firstName, String lastName) throws FinderException {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement("SELECT id FROM Doctor WHERE UPPER(firstName) LIKE UPPER(?) AND UPPER(lastName) LIKE UPPER(?)");
ps.setString(1, firstName);
ps.setString(2, lastName);
Vector doctors = new Vector();
ResultSet rset = ps.executeQuery();
while(rset.next()) {
Integer id = new Integer(rset.getInt(1));
doctors.addElement(id);
}
rset.close();
return doctors;
} catch (Exception e) {
throw new FinderException(e.getMessage());
} finally {
closeResources(ps, conn);
}
}
private void readFromDb(int id) throws CreateException {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getConnection();
ps = conn.prepareStatement("SELECT ID, FIRSTNAME, LASTNAME, SPECIALTY FROM Doctor WHERE id = ?");
ps.setInt(1, id);
ResultSet rset = ps.executeQuery();
if(!rset.next()) {
throw new FinderException("Doctor not found, id = " + id);
} else {
readFields(rset);
}
} catch (Exception e) {
throw new CreateException(e.getMessage());
} finally {
closeResources(ps, conn);
}
}
private void readFields(ResultSet rset) throws SQLException {
id = new Integer(rset.getInt(1));
firstName = rset.getString(2);
lastName = rset.getString(3);
specialty = rset.getString(4);
}
private void closeResources(PreparedStatement ps, Connection conn) throws EJBException {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (Exception e) {
throw new EJBException(e);
}
}
public Integer getId() { return id; }
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 getSpecialty() { return specialty; }
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
public DoctorVO getDoctorData() {
DoctorVO vo = new DoctorVO();
vo.setId(getId());
vo.setFirstName(getFirstName());
vo.setLastName(getLastName());
vo.setSpecialty(getSpecialty());
return vo;
}
public void setDoctorData(DoctorVO vo) {
//vo.setId(getId());
setFirstName(vo.getFirstName());
setLastName(vo.getLastName());
setSpecialty(vo.getSpecialty());
}
private Connection getConnection() {
try {
InitialContext ic = new InitialContext();
DataSource ds = (DataSource)ic.lookup(DATASOURCE);
return ds.getConnection();
} catch (Exception e) {
throw new EJBException(e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -