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

📄 jobbean.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 JobBean implements EntityBean
{
	private DataSource dataSource;
	private String ref;
	private String customer;
	private String description;
	private String location;

	public String getRef () {
		return ref;
	}

	public String getCustomer () {
		return customer;
	}

	public String getDescription () {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getLocation () {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	// EJB methods start here

	public void ejbPostCreate (String ref, String customer) {}

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

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

			stmt.setString(1, key.getRef());
			stmt.setString(2, key.getCustomer());
			rs = stmt.executeQuery();

			if (!rs.next()) {
				throw new FinderException("Unknown job: "+key);
			}
			return key;
		}
		catch (SQLException e) {
			error("Error in findByPrimaryKey for "+key,e);
		}
		finally {
			closeConnection(con, stmt, rs);
		}
		return null;
	}
	
	public Collection ejbFindByCustomer(String customer) throws FinderException {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(
			"SELECT ref, customer FROM Job WHERE customer = ? ORDER BY ref");

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

			Collection col = new ArrayList();
			while (rs.next()) {
				col.add(new JobPK(rs.getString(1),rs.getString(2)));
			}
			return col;
		}
		catch (SQLException e) {
			error("Error in findByCustomer: "+customer,e);
		}
		finally {
			closeConnection(con, stmt, rs);
		}
		return null;
	}

	public Collection ejbFindByLocation(String location) throws FinderException {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(
			"SELECT ref, customer FROM Job WHERE location = ? ORDER BY customer, ref");

			stmt.setString(1, location);
			rs = stmt.executeQuery();

			Collection col = new ArrayList();
			while (rs.next()) {
				col.add(new JobPK(rs.getString(1),rs.getString(2)));
			}
			return col;
		}
		catch (SQLException e) {
			error("Error in findByLocation: "+location,e);
		}
		finally {
			closeConnection(con, stmt, rs);
		}
		return null;
	}

	public void ejbHomeDeleteByCustomer(String customer) {
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = dataSource.getConnection();

			stmt = con.prepareStatement(
			"DELETE FROM Job WHERE customer = ?");

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

	public void ejbLoad(){
		JobPK key= (JobPK)ctx.getPrimaryKey();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(
			"SELECT description,location FROM Job WHERE ref = ? AND customer = ?");

			stmt.setString(1, key.getRef());
			stmt.setString(2, key.getCustomer());
			rs = stmt.executeQuery();

			if (!rs.next()) {
				error("No data found in ejbLoad for "+key,null);
			}
			this.ref = key.getRef();
			this.customer = key.getCustomer();
			this.description = rs.getString(1);
			this.location = rs.getString(2);
		}
		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 Job SET description = ?, location = ? WHERE ref = ? AND customer = ?");

			stmt.setString(1, description);
			stmt.setString(2, location);
			stmt.setString(3, ref);
			stmt.setString(4, customer);
			stmt.executeUpdate();
		}
		catch (SQLException e) {
			error("Error in ejbStore for "+ref+","+customer,e);
		}
		finally {
			closeConnection(con, stmt, null);
		}
	}

	public void ejbPassivate(){
		ref = null;
		customer = null;
		description = null;
		location = null;
	}

	public void ejbActivate(){
	}

	public void ejbRemove(){
		JobPK key = (JobPK)ctx.getPrimaryKey();
		
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = dataSource.getConnection();
		  
			stmt = con.prepareStatement(
			"DELETE FROM Job WHERE ref = ? and customer = ?");

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

⌨️ 快捷键说明

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