⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jobskillbean.java

📁 21天精通Java,这是一本英文书
💻 JAVA
字号:
package data;

import java.rmi.*;
import java.sql.*;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.sql.*;

public class JobSkillBean implements EntityBean
{
	private DataSource dataSource;
	private String job;
	private String customer;
	private String skill;

	public String getJob () {
		return job;
	}

	public String getCustomer () {
		return customer;
	}

	public String getSkill () {
		return skill;
	}

	// EJB methods start here

	public void ejbPostCreate (String job, String customer, String skill) {}

	public JobSkillPK ejbCreate (String job, String customer, String skill) throws CreateException {
		JobSkillPK key = new JobSkillPK(job,customer,skill);
		try {
			ejbFindByPrimaryKey(key);
			throw new CreateException("Duplicate job skill: "+key);
		}
		catch (FinderException ex) {}
		
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(
			"INSERT INTO JobSkill (job,customer,skill) VALUES (?,?,?)");

			stmt.setString(1, job);
			stmt.setString(2, customer);
			stmt.setString(3, skill);
			stmt.executeUpdate();
		}
		catch (SQLException e) {
			error("Error creating job skill "+key,e);
		}
		finally {
			closeConnection(con, stmt, null);
		}
		this.job = job;
		this.customer = customer;
		this.skill = skill;
		return key;
	}
	
	public JobSkillPK ejbFindByPrimaryKey(JobSkillPK key) throws FinderException {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(
			"SELECT job FROM JobSkill WHERE job = ? AND customer = ? AND skill = ?");

			stmt.setString(1, key.getJob());
			stmt.setString(2, key.getCustomer());
			stmt.setString(3, key.getSkill());
			rs = stmt.executeQuery();

			if (!rs.next()) {
				throw new FinderException("Unknown job skill: "+key);
			}
			return key;
		}
		catch (SQLException e) {
			error("Error in findByPrimaryKey for "+key,e);
		}
		finally {
			closeConnection(con, stmt, rs);
		}
		return null;
	}

	public Collection ejbFindByJob(String job, String customer) throws FinderException {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(
			"SELECT job, customer, skill FROM JobSkill WHERE job = ? AND customer = ? ORDER BY skill");

			stmt.setString(1, job);
			stmt.setString(2, customer);
			rs = stmt.executeQuery();

			Collection col = new ArrayList();
			while (rs.next()) {
				JobSkillPK key = new JobSkillPK(rs.getString(1),rs.getString(2),rs.getString(3));
				col.add(key);
			}
			return col;
		}
		catch (SQLException e) {
			error("Error in findJobSkills: "+job+","+customer,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 JobSkill WHERE job = ? and customer = ?");

			stmt.setString(1, job);
			stmt.setString(2, customer);
			stmt.executeUpdate();
		}
		catch (SQLException e) {
			error("Error removing all job skills "+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 JobSkill WHERE customer = ?");

			stmt.setString(1, customer);
			stmt.executeUpdate();
		}
		catch (SQLException e) {
			error("Error removing all customer skills "+customer,e);
		}
		finally {
			closeConnection(con, stmt, null);
		}
	}

	public void ejbLoad(){
		JobSkillPK key= (JobSkillPK)ctx.getPrimaryKey();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(
			"SELECT job FROM JobSkill WHERE job = ? AND customer = ? AND skill = ?");

			stmt.setString(1, key.getJob());
			stmt.setString(2, key.getCustomer());
			stmt.setString(3, key.getSkill());
			rs = stmt.executeQuery();

			if (!rs.next()) {
				error("No data found in ejbLoad for "+key,null);
			}
			this.job = key.getJob();
			this.customer = key.getCustomer();
			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(){
		job = null;
		customer = null;
		skill = null;
	}

	public void ejbActivate(){
	}

	public void ejbRemove(){
		JobSkillPK key = (JobSkillPK)ctx.getPrimaryKey();
		
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = dataSource.getConnection();
		  
			stmt = con.prepareStatement(
			"DELETE FROM JobSkill WHERE job = ? and customer = ? AND skill = ?");

			stmt.setString(1, key.getJob());
			stmt.setString(2, key.getCustomer());
			stmt.setString(3, key.getSkill());
			stmt.executeUpdate();
		}
		catch (SQLException e) {
			error("Error removing job skill "+key,e);
		}
		finally {
			closeConnection(con, stmt, null);
		}
		job = null;
		customer = 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 = "JobSkillBean: "+msg + "\n" + ex;
		System.out.println(s);
		throw new EJBException(s,ex);
	}
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -