📄 applicantskillbean.java
字号:
package data;
import java.rmi.*;
import java.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.*;
public class ApplicantSkillBean implements EntityBean
{
private DataSource dataSource;
private String applicant;
private String skill;
public String getApplicant () {
return applicant;
}
public String getSkill () {
return skill;
}
// EJB methods start here
public void ejbPostCreate (String applicant, String skill) {}
public ApplicantSkillPK ejbCreate (String applicant, String skill) throws CreateException {
ApplicantSkillPK key = new ApplicantSkillPK(applicant,skill);
try {
ejbFindByPrimaryKey(key);
throw new CreateException("Duplicate applicant skill: "+key);
}
catch (FinderException ex) {}
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"INSERT INTO ApplicantSkill (applicant,skill) VALUES (?,?)");
stmt.setString(1, applicant);
stmt.setString(2, skill);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error creating applicant skill "+key,e);
}
finally {
closeConnection(con, stmt, null);
}
this.applicant = applicant;
this.skill = skill;
return key;
}
public ApplicantSkillPK ejbFindByPrimaryKey(ApplicantSkillPK key) throws FinderException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT applicant FROM ApplicantSkill WHERE applicant = ? AND skill = ?");
stmt.setString(1, key.getApplicant());
stmt.setString(2, key.getSkill());
rs = stmt.executeQuery();
if (!rs.next()) {
throw new FinderException("Unknown applicant skill: "+key);
}
return key;
}
catch (SQLException e) {
error("Error in findByPrimaryKey for "+key,e);
}
finally {
closeConnection(con, stmt, rs);
}
return null;
}
public Collection ejbFindByApplicant (String applicant) throws FinderException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT applicant, skill FROM ApplicantSkill WHERE applicant = ? ORDER BY skill");
stmt.setString(1, applicant);
rs = stmt.executeQuery();
Collection col = new ArrayList();
while (rs.next()) {
ApplicantSkillPK key = new ApplicantSkillPK(rs.getString(1),rs.getString(2));
col.add(key);
}
return col;
}
catch (SQLException e) {
error("Error in find by applicant: "+applicant,e);
}
finally {
closeConnection(con, stmt, rs);
}
return null;
}
public void ejbHomeDeleteByApplicant(String applicant) {
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"DELETE FROM ApplicantSkill WHERE applicant = ?");
stmt.setString(1, applicant);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error removing all applicant skills "+applicant,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void ejbLoad(){
ApplicantSkillPK key= (ApplicantSkillPK)ctx.getPrimaryKey();
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT applicant FROM ApplicantSkill WHERE applicant = ? AND skill = ?");
stmt.setString(1, key.getApplicant());
stmt.setString(2, key.getSkill());
rs = stmt.executeQuery();
if (!rs.next()) {
error("No data found in ejbLoad for "+key,null);
}
this.applicant = key.getApplicant();
this.skill = key.getSkill();
}
catch (SQLException e) {
error("Error in ejbLoad for "+key,e);
}
finally {
closeConnection(con, stmt, rs);
}
}
public void ejbStore(){
// immutable object so nothing to store
}
public void ejbPassivate(){
applicant = null;
skill = null;
}
public void ejbActivate(){
}
public void ejbRemove(){
ApplicantSkillPK key = (ApplicantSkillPK)ctx.getPrimaryKey();
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"DELETE FROM ApplicantSkill WHERE applicant = ? AND skill = ?");
stmt.setString(1, key.getApplicant());
stmt.setString(2, key.getSkill());
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error removing applicant skill "+key,e);
}
finally {
closeConnection(con, stmt, null);
}
applicant = null;
skill = null;
}
private EntityContext ctx;
public void setEntityContext(EntityContext ctx) {
this.ctx = ctx;
InitialContext ic = null;
try {
ic = new InitialContext();
dataSource = (DataSource)ic.lookup("java:comp/env/jdbc/Agency");
}
catch (NamingException ex) {
error("Error connecting to java:comp/env/jdbc/Agency:",ex);
return;
}
}
public void unsetEntityContext() {
this.ctx = null;
dataSource = null;
}
private void closeConnection (Connection con, PreparedStatement stmt, ResultSet rslt) {
if (rslt != null) {
try {
rslt.close();
}
catch (SQLException e) {}
}
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException e) {}
}
if (con != null) {
try {
con.close();
}
catch (SQLException e) {}
}
}
private void error (String msg, Exception ex) {
String s = "ApplicantSkillBean: "+msg + "\n" + ex;
System.out.println(s);
throw new EJBException(s,ex);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -