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

📄 newcustomerbean.java

📁 J2EE开发与Weblogic一书中的源代码
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -