?? matchedbean.java
字號:
package data;
import java.rmi.*;
import java.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.*;
public class MatchedBean implements EntityBean
{
private DataSource dataSource;
private String job;
private String customer;
private String applicant;
private boolean exact;
public String getJob () {
return job;
}
public String getCustomer () {
return customer;
}
public String getApplicant () {
return applicant;
}
public boolean getExact () {
return exact;
}
public void setExact(boolean exact) {
this.exact = exact;
}
// EJB methods start here
public void ejbPostCreate (String applicant, String job, String customer, boolean exact) {}
public MatchedPK ejbCreate (String applicant, String job, String customer, boolean exact) throws CreateException {
MatchedPK key = new MatchedPK(applicant,job,customer);
try {
ejbFindByPrimaryKey(key);
throw new CreateException("Duplicate matched entry: "+key);
}
catch (FinderException ex) {}
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"INSERT INTO Matched (applicant,job,customer,exact) VALUES (?,?,?,?)");
stmt.setString(1, applicant);
stmt.setString(2, job);
stmt.setString(3, customer);
stmt.setBoolean(4, exact);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error creating matched entry "+key,e);
}
finally {
closeConnection(con, stmt, null);
}
this.applicant = applicant;
this.job = job;
this.customer = customer;
this.exact = exact;
return key;
}
public MatchedPK ejbFindByPrimaryKey(MatchedPK key) throws FinderException {
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT exact FROM Matched WHERE applicant = ? AND job = ? AND customer = ?");
stmt.setString(1, key.getApplicant());
stmt.setString(2, key.getJob());
stmt.setString(3, key.getCustomer());
rs = stmt.executeQuery();
if (!rs.next()) {
throw new FinderException("Unknown matched entry: "+key);
}
return key;
}
catch (SQLException e) {
error("Error in findByPrimaryKey for "+key,e);
}
finally {
closeConnection(con, stmt, rs);
}
return null;
}
public void ejbHomeDeleteByJob(String job, String customer) {
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"DELETE FROM Matched WHERE job = ? and customer = ?");
stmt.setString(1, job);
stmt.setString(2, customer);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error removing all matched job lines "+job+":"+customer,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void ejbHomeDeleteByCustomer(String customer) {
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"DELETE FROM Matched WHERE customer = ?");
stmt.setString(1, customer);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error removing all customer matched entries "+customer,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void ejbHomeDeleteByApplicant(String applicant) {
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"DELETE FROM Matched WHERE applicant = ?");
stmt.setString(1, applicant);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error removing all applicant matched entries "+applicant,e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void ejbLoad(){
MatchedPK key= (MatchedPK)ctx.getPrimaryKey();
Connection con = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"SELECT exact FROM Matched WHERE applicant = ? AND job = ? AND customer = ?");
stmt.setString(1, key.getApplicant());
stmt.setString(2, key.getJob());
stmt.setString(3, key.getCustomer());
rs = stmt.executeQuery();
if (!rs.next()) {
error("No data found in ejbLoad for "+key,null);
}
this.applicant = key.getApplicant();
this.job = key.getJob();
this.customer = key.getCustomer();
this.exact = rs.getBoolean(1);
}
catch (SQLException e) {
error("Error in ejbLoad for "+key,e);
}
finally {
closeConnection(con, stmt, rs);
}
}
public void ejbStore(){
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"UPDATE Matched SET exact = ? WHERE applicant = ? AND job = ? AND customer = ?");
stmt.setBoolean(1, this.exact);
stmt.setString(2, this.applicant);
stmt.setString(3, this.job);
stmt.setString(4, this.customer);
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error in ejbStore for "+ctx.getPrimaryKey(),e);
}
finally {
closeConnection(con, stmt, null);
}
}
public void ejbPassivate(){
applicant = null;
job = null;
customer = null;
exact = false;
}
public void ejbActivate(){
}
public void ejbRemove(){
MatchedPK key = (MatchedPK)ctx.getPrimaryKey();
Connection con = null;
PreparedStatement stmt = null;
try {
con = dataSource.getConnection();
stmt = con.prepareStatement(
"DELETE FROM Matched WHERE applicant = ? AND job = ? AND customer = ?");
stmt.setString(1, key.getApplicant());
stmt.setString(2, key.getJob());
stmt.setString(3, key.getCustomer());
stmt.executeUpdate();
}
catch (SQLException e) {
error("Error removing matched entry "+key,e);
}
finally {
closeConnection(con, stmt, null);
}
applicant = null;
job = null;
customer = null;
exact = false;
}
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 = "MatchedBean: "+msg + "\n" + ex;
System.out.println(s);
throw new EJBException(s,ex);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -