newcustomerbean.java

来自「J2EE开发与Weblogic一书中的源代码」· Java 代码 · 共 86 行

JAVA
86
字号
package com.learnweblogic.examples.ch10.customer;

import java.rmi.RemoteException;

import javax.ejb.CreateException;
import javax.ejb.DuplicateKeyException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

public class NewCustomerBean implements SessionBean {

	private SessionContext ctx;

	private CustomerHome customerHome;

	public void setSessionContext(SessionContext c) {
		ctx = c;

		try {
			Context ic = new InitialContext();
			Object h = ic.lookup("java:/comp/env/ejb/CustomerHome");

			customerHome =
				(CustomerHome) PortableRemoteObject.narrow(
					h,
					CustomerHome.class);

		} catch (NamingException ne) {
			ne.printStackTrace();
			throw new EJBException(ne);
		}
	}

	public void ejbCreate() {
	}
	public void ejbRemove() {
	}

	public void ejbActivate() {
	}
	public void ejbPassivate() {
	}

	public void enterNewCustomer(
		int id,
		String firstName,
		String lastName,
		String emailAddress)
		throws InvalidCustomerException {
		Integer key = new Integer(id);
		Customer customer = null;
		// Poll database to determine whether a
		// record with the primary key exists.
		// Don't try to create one if it already
		// exists because BEA/Pointbase gets tied
		// in a knot on the rollback of the insert.
		try {
			customer = customerHome.findByPrimaryKey(key);
		} catch (FinderException fe) {
			// The customer record does not exist.
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		try {
			if (null == customer) {
				customerHome.create(key, firstName, lastName, emailAddress);
			} else {
				throw new InvalidCustomerException(
					"Customer with id: " + id + " already exists.");
			}
		} catch (DuplicateKeyException dke) {
			throw new InvalidCustomerException(
				"Customer with id: " + id + " already exists.");
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (CreateException e) {
			e.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

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