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

📄 catalogbean.java

📁 使用JBulider开发的java实例。非常全。
💻 JAVA
字号:
package cmp2image;

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

public class CatalogBean
    implements SessionBean {
  SessionContext sessionContext;
  public void ejbCreate() throws CreateException {
    /**@todo Complete this method*/
  }

  public void ejbRemove() {
    /**@todo Complete this method*/
  }

  public void ejbActivate() {
    /**@todo Complete this method*/
  }

  public void ejbPassivate() {
    /**@todo Complete this method*/
  }

  public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
  }

  public ArrayList findOne(String name) {
    ArrayList collection = new ArrayList();
    try {
      Context context = getInitialContext();
      //look up jndi name
      Object ref = context.lookup("Flower");
      //look up jndi name and cast to Home interface
      FlowerHome flowerHome = (FlowerHome) PortableRemoteObject.narrow(
          ref, FlowerHome.class);
      Flower flower = flowerHome.findByPrimaryKey(name);
      System.out.println("Name in Bean: " + flower.getName());
      collection.add(flower.getName());
      collection.add(flower.getImgbytes());
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return collection;
  }

  public ArrayList getAll() {
    ArrayList collection = new ArrayList();
    try {
      Context context = getInitialContext();

      //look up jndi name
      Object ref = context.lookup("Flower");
      //look up jndi name and cast to Home interface
      FlowerHome flowerHome = (FlowerHome) PortableRemoteObject.narrow(
          ref, FlowerHome.class);
      Collection flowers = flowerHome.findByAll();
      Iterator i = flowers.iterator();
      while (i.hasNext()) {
        Flower flower = (Flower) i.next();
        collection.add(flower.getName());
        collection.add(flower.getImgbytes());
      }

    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    return collection;

  }

  private Context getInitialContext() throws Exception {
    String url = "t3://localhost:7001";
    String user = null;
    String password = null;
    Properties properties = null;
    try {
      properties = new Properties();
      properties.put(Context.INITIAL_CONTEXT_FACTORY,
                     "weblogic.jndi.WLInitialContextFactory");
      properties.put(Context.PROVIDER_URL, url);
      if (user != null) {
        properties.put(Context.SECURITY_PRINCIPAL, user);
        properties.put(Context.SECURITY_CREDENTIALS,
                       password == null ? "" : password);
      }

      return new InitialContext(properties);
    }
    catch (Exception e) {
      System.out.println("Unable to connect to WebLogic server at " + url);
      System.out.println("Please make sure that the server is running.");
      throw e;
    }
  }

}

⌨️ 快捷键说明

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