?? client.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 + -