studentbmpclient.java

来自「java的一系列产品中包括jsme,jmse,j2ee,本文件提供j2ee实现的」· Java 代码 · 共 78 行

JAVA
78
字号
package bmp;

import bmp.BaseClient;

import javax.ejb.DuplicateKeyException;
import javax.naming.NamingException;
import java.rmi.RemoteException;

public final class StudentBMPClient extends BaseClient {

  private StudentHome studentHome;

  public StudentBMPClient(String [] argv) throws NamingException
  {
    super(argv);    
    try {
      studentHome = (StudentHome) 
        narrow(getInitialContext().lookup("StudentEJB"), StudentHome.class);

    } catch (NamingException ne) {
      System.err.println("Unable to lookup EJB: StudentEJB");
      System.err.println("Please make sure that you have deployed this EJB.");
      throw ne;
    }
  }

  private void runExample() throws Exception
  {
    Student s = null;

    try {
      s = studentHome.create("John Doe", 202, 10);
      System.err.println("Created a John Doe Student BMP Entity.");

    } catch (DuplicateKeyException dke) {
      System.err.println("Student named John Doe already exists.");
      System.err.println("Remove the John Doe bean from your "+
        "database table and re-run the example.");
      throw dke;
    } catch (Exception e) {
      System.err.println("Unable to create Student bean.");
      System.err.println("Did you create the database tables?");
      throw e;
    }

    try {
      s.setGrade(4);
      
      System.err.println("Changed student's grade to 4.");
    } catch (RemoteException re) {
      System.err.println("Unable to update student's grade.");
      re.printStackTrace();
      throw re;
    }

    try {
      s.remove();

    } catch (Exception e) {
      System.err.println("Unable to remove Student bean.");
      e.printStackTrace();
      throw e;
    }
  }

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

    System.err.println("");
    System.err.println("Running StudentBMPClient.....");
    System.err.println("");

    StudentBMPClient sc = new StudentBMPClient(argv);
    sc.runExample();
    System.err.println("");
  }
}

⌨️ 快捷键说明

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