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

📄 simpleejb.java

📁 此程序都是企业级 的数据库开发程序 全面揭示了JAVA对数据库的操作
💻 JAVA
字号:
// import COM.cloudscape.core.*;   // Core database classes
import java.sql.*;
import java.util.Properties;
import java.util.Collection;
import java.util.Iterator;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import javax.naming.*;
import javax.ejb.CreateException;

import ejb.recording.*;


/**
 * A driver program for EJB examples
 */
public class SimpleEJB {

  public static void main(String[] args) {

    /**
     * We need these properties to connect with the container
     */
    Properties containerEnvironment = new Properties();
    containerEnvironment
      .setProperty("java.naming.factory.initial", 
                   "org.jnp.interfaces.NamingContextFactory");
    containerEnvironment.setProperty("java.naming.provider.url", 
                                     "localhost:1099");
    containerEnvironment.setProperty("java.naming.factory.url.pkgs", 
                                     "org.jboss.naming");

    InitialContext jndiContext = null;
    Object reference = null;
    try {

      // naming context comment
      jndiContext = new InitialContext(containerEnvironment);

      // Get a reference to the Recording Bean
      reference = jndiContext.lookup("Recording");
    } catch (NamingException ne) {
      System.out.println("Naming exception:");
      ne.printStackTrace();
    } 

    // Narrow
    RecordingHome home = 
      (RecordingHome) PortableRemoteObject.narrow(reference, 
            RecordingHome.class);

    // Use the home interface to get a recording object

    Recording rec1 = null;
    Recording rec2 = null;
    Recording rec3 = null;
    Recording rec4 = null;

    // Create the same Recordings as the OR chapter:
    try {
      rec1 = home.create(new Integer(1), "392-000022", new Double(11.99), 
                         "Bryan Adams", "Cuts Like A Knife");
      rec2 = home.create(new Integer(2), "RS-32-1133", new Double(10.99), 
                         "Rick Springfield", "Working Class Dog");
      rec3 = home.create(new Integer(3), "303-9399293", new Double(11.99), 
                         "Petra", "Come And Join Us");
      rec4 = home.create(new Integer(4), "BK-30-23993", new Double(13.99), 
                         "Bruce Springsteen", "Lucky Town");

      /**
       * The code for the client application looks very similar to the
       * driver program in the object-relational chapter, doesn't it?
       * The way the Recording class is implemented is a bit different
       */

    } catch (RemoteException re) {
      re.printStackTrace();
    } catch (CreateException ce) {
      ce.printStackTrace();
    } 

    // System.out.println("created");

    /**
     * Now try to find a recording using EJB
     */
    try {
      Collection c = home.findByArtist("Bryan%");

      // System.out.println("collection");
      Iterator iter = c.iterator();

      // System.out.println("iterator");
      while (iter.hasNext()) {
        Recording temp = (Recording) iter.next();

        // System.out.println("next");
        System.out.println("Found: " + temp.getRecordingTitle() + " by " 
                           + temp.getRecordingArtist());
      } 
    } catch (Exception e) {
      System.out.println("No Recordings found!");
    } 

    /**
     * Get rid of the Recordings
     */
    try {
      rec1.remove();
      rec2.remove();
      rec3.remove();
      rec4.remove();
    } catch (Exception e)   // Note, these methods really throw
 
    // RemoteException and RemoveException
    {
      e.printStackTrace();
    } 

  } 

}

⌨️ 快捷键说明

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