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

📄 applicantbean.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 ApplicantBean implements EntityBean
{
	private DataSource dataSource;
	private String login;
	private String name;
	private String email;
	private String summary;
	private String location;

	public String getLogin () {
		return login;
	}

	public String getName () {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getEmail () {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSummary () {
		return summary;
	}

	public void setSummary(String summary) {
		this.summary = summary;
	}

	public String getLocation () {
		return location;
	}

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

	// EJB methods start here

	public void ejbPostCreate (String login, String customer, String email) {}

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

			stmt.setString(1, login);
			stmt.setString(2, name);
			stmt.setString(3, email);
			stmt.executeUpdate();
		}
		catch (SQLException e) {
			error("Error creating applicant "+login,e);
		}
		finally {
			closeConnection(con, stmt, null);
		}
		this.login = login;
		this.name = name;
		this.email = email;
		this.summary = null;
		this.location = null;
		return login;
	}

	public String ejbFindByPrimaryKey(String login) throws FinderException {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(
			"SELECT login FROM Applicant WHERE login = ?");

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

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

	public Collection ejbFindAll() throws FinderException {
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(
			"SELECT login FROM Applicant ORDER By login");

			rs = stmt.executeQuery();

			Collection col = new ArrayList();
			while (rs.next()) {
				col.add(rs.getString(1));
			}
			return col;
		}
		catch (SQLException e) {
			error("Error in findAll",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 login FROM Applicant WHERE location = ? ORDER BY login");

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

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

	public void ejbLoad(){
		String login = (String)ctx.getPrimaryKey();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(
			"SELECT name,email,summary,location FROM Applicant WHERE login = ?");

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

			if (!rs.next()) {
				error("No data found in ejbLoad for "+login,null);
			}
			this.login = login;
			this.name = rs.getString(1);
			this.email = rs.getString(2);
			this.summary = rs.getString(3);
			this.location = rs.getString(4);
		}
		catch (SQLException e) {
			error("Error in ejbLoad for "+login,e);
		}
		finally {
			closeConnection(con, stmt, rs);
		}
	}

	public void ejbStore(){
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = dataSource.getConnection();
			stmt = con.prepareStatement(
			"UPDATE Applicant SET name = ?, email = ?, summary = ?, location = ? WHERE login = ?");

			stmt.setString(1, name);
			stmt.setString(2, email);
			stmt.setString(3, summary);
			stmt.setString(4, location);
			stmt.setString(5, login);
			stmt.executeUpdate();
		}
		catch (SQLException e) {
			error("Error in ejbStore for "+login,e);
		}
		finally {
			closeConnection(con, stmt, null);
		}
	}

	public void ejbPassivate(){
		login = null;
		name = null;
		email = null;
		summary = null;
		location = null;
	}

	public void ejbActivate(){
	}

	public void ejbRemove(){
		String login = (String)ctx.getPrimaryKey();
		
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = dataSource.getConnection();
		  
			stmt = con.prepareStatement(
			"DELETE FROM Applicant WHERE login = ?");

			stmt.setString(1, login);
			stmt.executeUpdate();
		}
		catch (SQLException e) {
			error("Error removing applicant "+login,e);
		}
		finally {
			closeConnection(con, stmt, null);
		}
		login = null;
		name = null;
		email = null;
		summary = 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 = "ApplicantBean: "+msg + "\n" + ex;
		System.out.println(s);
		throw new EJBException(s,ex);
	}
	
}

⌨️ 快捷键说明

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