?? accountclient.java
字號:
package Account;
import javax.naming.InitialContext;
import javax.naming.Context;
import java.util.Hashtable;
import java.util.Iterator;
public class AccountClient {
public static void main(String[] args) throws Exception {
Account account = null;
try { // 獲取一個本地對象的引用
Properties props = System.getProperties();
Context ctx = new InitialContext(props);
Object obj = ctx.lookup("Account");
AccountHome home = (AccountHome)obj;
System.err.println("Total of all accounts in bank initially = " +
home.getTotalBankValue());
// 生成EJB對象
home.create("123-456-7890", "John Smith");
// 查找一個賬戶
Iterator i = home.findByOwnerName("John Smith").iterator();
if (i.hasNext()) {
account = (Account)i.next();
javax.rmi.PortableRemoteObject.narrow(i.next(),Account.class);
}
else {
throw new Exception("Could not find account");
}
// 調用balance()方法,并打印輸出
System.out.println("Initial Balance = " + account.getBalance());
// 存款100
account.deposit(100);
System.out.println("After depositing 100, account balance = " +
account.getBalance());
System.out.println("Total of all accounts in bank now = " +
home.getTotalBankValue());
// 提取EJB對象的主鍵
AccountPK pk = (AccountPK) account.getPrimaryKey();
// 釋放我們的老的EJB對象的引用,通過ID主鍵號查找
account = null;
account = home.findByPrimaryKey(pk);
// 打印現有的余額
System.out.println("Found account with ID " + pk +
" Balance = " + account.getBalance());
//取款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!\n" + e);
// e.printStackTrace();
}
finally {// 永久的刪除實體
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 + -