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

📄 client.java

📁 EJB_原代码多例-好好东西啊
💻 JAVA
字号:
package com.wiley.compBooks.roman.entity.account;

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

/**
 * Sample client code which manipulates a Bank Account Entity Bean.
 */
public class Client {

	public static void main(String[] args) {

		Account account = null;

		try {
			/*
			 * Get a reference to the Account Home Object - the
			 * factory for Account EJB Objects
			 */
			Context ctx = new InitialContext(System.getProperties());
			AccountHome home =
			(AccountHome) ctx.lookup("AccountHome");

			/*
			 * Use the factory to create the Account EJB Object
			 */
			home.create("123-456-7890", "John Smith");

			/*
			 * Find an account
			 */
			Enumeration e = home.findByOwnerName("John Smith");
			if (e != null) {
				account = (Account) e.nextElement();
			}
			else {
				throw new Exception("Could not find account");
			}

			/*
			 * Call the balance() method, and print it
			 */
			System.out.println("Initial Balance = " + account.getBalance());

			/*
			 * Deposit $100 into the account
			 */
			account.deposit(100);

			/*
			 * Retrieve the resulting balance.
			 */
			System.out.println("After depositing 100, account balance = " + account.getBalance());

			/*
			 * Retrieve the Primary Key from the EJB Object
			 */
			AccountPK pk = (AccountPK) account.getPrimaryKey();

			/*
			 * Release our old EJB Object reference.  Now call
			 * find() again, this time querying on Account ID
			 * (i.e. the Primary Key).
			 */
			account = null;
			account = home.findByPrimaryKey(pk);

			/*
			 * Print out current balance
			 */
			System.out.println("Found account with ID " + pk + ".  Balance = " + account.getBalance());

			/*
			 * Try to withdraw $150
			 */
			System.out.println("Now trying to withdraw $150, which is more than is currently available.  This should generate an exception..");
			account.withdraw(150);

		}
		catch (Exception e) {
			System.out.println("Caught exception!");
			e.printStackTrace();
		}
		finally {
			/*
			 * Destroy the Entity permanently
			 */
			try {
				System.out.println("Destroying account..");
				if (account != null) {
					account.remove();
				}
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}

⌨️ 快捷键说明

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