cmp-ProductClient.java

本文介绍了一个使用Java EE中的EJB技术实现的产品客户端应用程序示例。该客户端通过远程调用方式与产品实体Bean交互,演示了如何创建、查找及删除产品记录。此外,还展示了如何利用EJB提供的业务逻辑来查询特定条件下的产品。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package examples;

 

import javax.ejb.*;

import javax.naming.*;

import java.rmi.*;

import java.util.*;

 

/**

 * Client test application on a Container-Managed Entity Bean, Product.

 */

public class ProductClient {

 

 public static void main(String[] args) throws Exception {

 

  ProductHome home = null;

 

  try {

   /*

    * Get a reference to the Product Home Object - the

    * factory for Product EJB Objects

    */

   Context ctx = new InitialContext(System.getProperties());

   home = (ProductHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("RemoteProductHome"), ProductHome.class);

 

   /*

    * Use the factory to create the Product EJB Object

    */

   home.create("123-456-7890", "P5-350", "350 Mhz Pentium", 200);

   home.create("123-456-7891", "P5-400", "400 Mhz Pentium", 300);

   home.create("123-456-7892", "P5-450", "450 Mhz Pentium", 400);

   home.create("123-456-7893", "SD-64", "64 MB SDRAM", 50);

   home.create("123-456-7894", "SD-128", "128 MB SDRAM", 100);

   home.create("123-456-7895", "SD-256", "256 MB SDRAM", 200);

 

   /*

    * Find a Product, and print out it's description

    */

   Iterator i = home.findByName("SD-64").iterator();

   System.out.println("The following product descriptions match the product name SD-64:");

   while (i.hasNext()) {

    Product prod = (Product) javax.rmi.PortableRemoteObject.narrow(i.next(), Product.class);

    System.out.println(prod.getDescription());

   }

 

   /*

    * Find all products that cost $200

    */

   System.out.println("Calling finder to find all products that cost $200");

   i = home.findByBasePrice(200).iterator();

 

   while (i.hasNext()) {

    Product prod = (Product) javax.rmi.PortableRemoteObject.narrow(i.next(), Product.class);

    System.out.println(prod.getDescription());

   }

  }

  catch (Exception e) {

   e.printStackTrace();

  }

  finally {

   if (home != null) {

    System.out.println("Destroying products..");

 

    /*

     * Find all the products

     */

    Iterator i = home.findAllProducts().iterator();

    while (i.hasNext()) {

     try {

      Product prod = (Product) javax.rmi.PortableRemoteObject.narrow(i.next(), Product.class);

      if (prod.getProductID().startsWith("123")) {

       prod.remove();

      }

     }

     catch (Exception e) {

      e.printStackTrace();

     }

    }

   }

  }

 }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值