可以呀,我这么实现过其实是一样的查询
就是要初始化Context                       Properties p = new Properties();
                       p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.TengahInitialContextFactory");
                       p.put(Context.PROVIDER_URL, "t3://localhost:7001");  
                                                 //指定你的应用服务器的地址
context = new InitialContext(p);
} catch (NamingException ne){
                        ne.printStackTrace();
throw new ServiceLocatorException("context can't intial");  
}以下是一样的查询、调用

解决方案 »

  1.   

    楼上的没错
    你只需要根据你的SERVER的不同,写相应的Properties
    其他的都和以前一样
      

  2.   

    同意楼上,不知道你的EJB服务器是什么,
    如果是JBoss用
    Context ctx = null;
    Properties ht = new Properties();
    ht.put(ctx.PROVIDER_URL, "ip:1099" );
    ht.put(ctx.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    ctx = new InitialContext(ht);Object objRef = ctx.lookup("Salary");
      

  3.   

    weblogic 7.0+JBuilderpackage demo1;import javax.naming.*;
    import java.util.Properties;
    import javax.rmi.PortableRemoteObject;public class HelloBeanTestClient1
        extends Object {
      private static final String ERROR_NULL_REMOTE = "Remote interface reference is null.  It must be created by calling one of the Home interface methods first.";
      private static final int MAX_OUTPUT_LINE_LENGTH = 100;
      private boolean logging = true;
      private HelloHome helloHome = null;
      private Hello hello = null;  //Construct the EJB test client
      public HelloBeanTestClient1() {
        initialize();
      }  public void initialize() {
        long startTime = 0;
        if (logging) {
          log("Initializing bean access.");
          startTime = System.currentTimeMillis();
        }    try {
          //get naming context
          Context context = getInitialContext();      //look up jndi name
          Object ref = context.lookup("HelloBean");
          //look up jndi name and cast to Home interface
          helloHome = (HelloHome) PortableRemoteObject.narrow(ref, HelloHome.class);
          if (logging) {
            long endTime = System.currentTimeMillis();
            log(
                "Succeeded initializing local bean access through Local Home interface.");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch (Exception e) {
          if (logging) {
            log("Failed initializing bean access.");
          }
          e.printStackTrace();
        }
      }  private Context getInitialContext() throws Exception {
        String url = "t3://ghost: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) {
          log("Unable to connect to WebLogic server at " + url);
          log("Please make sure that the server is running.");
          throw e;
        }
      }  //----------------------------------------------------------------------------
      // Methods that use Home interface methods to generate a Remote interface reference
      //----------------------------------------------------------------------------  public Hello create() {
        long startTime = 0;
        if (logging) {
          log("Calling create()");
          startTime = System.currentTimeMillis();
        }
        try {
          hello = helloHome.create();
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded: create()");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch (Exception e) {
          if (logging) {
            log("Failed: create()");
          }
          e.printStackTrace();
        }    if (logging) {
          log("Return value from create(): " + hello + ".");
        }
        return hello;
      }  //----------------------------------------------------------------------------
      // Utility Methods
      //----------------------------------------------------------------------------  private void log(String message) {
        if (message == null) {
          System.out.println("-- null");
          return;
        }
        if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
          System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) +
                             " ...");
        }
        else {
          System.out.println("-- " + message);
        }
      }  //Main method  public static void main(String[] args) {
        try {
          HelloBeanTestClient1 client = new HelloBeanTestClient1();
          // Use the client object to call one of the Home interface wrappers
          // above, to create a Remote interface reference to the bean.
          // If the return value is of the Remote interface type, you can use it
          // to access the remote interface methods.  You can also just use the
          // client object to call the Remote interface wrappers.
          String out = client.create().hello();
          System.out.println(out);
        }
        catch (Exception e) {
          e.printStackTrace();
        }
      }}
      

  4.   

    javac -classpath ".;weblogic.jar" test\TestClient.java 
    java -classpath ".;weblogic.jar" test.TestClienttest中的文件清单:
    testRemote.class   〈〈〈-----EJB远程接口
    testRemoteHome.class  〈〈〈-----EJB Home接口
    TestClient.java        〈〈〈-----客户程序我用的是weblogic, 所以weblogic.jar要在类路径中
      

  5.   

    TO: Kylix_XP(风的流浪) 嘿嘿,你的名字和我的正好相反