 |
返回文章
CatClient.java,通过 Geronimo 客户机应用程序容器访问 CategoryEntityBean 以获得种类数据
package com.ibm.dw.reallybigpet.client;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import com.ibm.dw.reallybigpet.ejb.cmp.CategoryHome;
import com.ibm.dw.reallybigpet.ejb.cmp.CategoryRemote;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.rmi.PortableRemoteObject;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
public class CatClient extends JFrame {
public CatClient(String title) {
super(title);
}
public void init() {
JList catList = new JList(getCats());
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
pane.add(catList, BorderLayout.CENTER);
this.getContentPane().add(pane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private Vector getCats() {
Vector cats = new Vector();
Collection coll = null;
CategoryRemote cr = null;
try {
Context ic = new InitialContext();
Object o = ic.lookup("java:comp/env/ejb/CategoryEntityBean");
CategoryHome home = (CategoryHome) PortableRemoteObject.narrow(o,
CategoryHome.class);
coll = home.findAll();
Iterator it = coll.iterator();
while (it.hasNext()) {
cr = (CategoryRemote) it.next();
cats.add("Cat id= " + cr.getId() + "; Name= " + cr.getName());
} // of while
} catch (Exception ex) {
ex.printStackTrace();
}
return cats;
}
public static void main(String[] args) {
CatClient lc = new CatClient("Really Big Pet Store Categories");
lc.init();
lc.setSize(350, 150);
lc.setVisible(true);
}
}
|
返回文章
|  |
|