English 中文(简体)
当直接运行帮手类时, 代码会丢弃一个例外
原标题:code throws an exception when i directly run the helper class

以下代码从数据库中获取用户电子邮件中的照片名称 。

package NonServletFiles;
import javax.sql.*;
import java.sql.*;
import javax.naming.*;
public class GetPhotosForTheUser {

public ResultSet getData(String email) {
    ResultSet set = null;
    try {
        String sqlQuery = "select nameofthephoto from photocaptions where useremail= " + email + " ";
        Context context = new InitialContext();
        DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/photog"); // LINE 17
        Connection connection = ds.getConnection();
        PreparedStatement statement = connection.prepareStatement(sqlQuery);
        set = statement.executeQuery();
        while(set.next()){
            System.out.println("Name Of The Photo : " + set.getString("NameOfThePhoto"));
        }
    }catch(Exception exc) {
        exc.printStackTrace();
    }

    return set;
  }
}

若我从 .jsp 文件调用此类助手为 :

    <% 
        GetPhotosForTheUser gpftu = new GetPhotosForTheUser();
        gpftu.getData("[email protected]");
    %>

它在服务器控制台上打印正确的名称 。

但如果我单靠添加 main 方法来使用此类

""https://i.sstatic.net/K63ks.png" alt="此处的内置图像描述"/"

在那级助手中,他抛出一种例外,即:

javax.naming.NamingException: Lookup failed for  java:comp/env/jdbc/photog  in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is javax.naming.NamingException: Invocation exception: Got null ComponentInvocation ]
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at NonServletFiles.GetPhotosForTheUser.getData(GetPhotosForTheUser.java:17)
at NonServletFiles.GetPhotosForTheUser.main(GetPhotosForTheUser.java:32)
    Caused by: javax.naming.NamingException: Invocation exception: Got null ComponentInvocation 
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.getComponentId(GlassfishNamingManagerImpl.java:873)
at com.sun.enterprise.naming.impl.GlassfishNamingManagerImpl.lookup(GlassfishNamingManagerImpl.java:742)
at com.sun.enterprise.naming.impl.JavaURLContext.lookup(JavaURLContext.java:172)
at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:498)
... 4 more

我使用玻璃鱼服务机和网豆来开发

问题回答

当您作为网络应用程序运行此功能时, 上下文会以服务器细节初始化

Context context = new InitialContext(); // This is initialized when you run as web app

当您作为独立程序运行时, 调用主方法时, 情况不同 。

您可以通过初始化上下文来摆脱这种情况。

Properties prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, "your provider"); // like for websphere it is com.ibm.websphere.naming.WsnInitialContextFactory and weblogic weblogic.jndi.WLInitialContextFactory
prop.put(Context.PROVIDER_URL, "server path"); //
Context context = new InitialContext(prop);

注意: 通常您不会写成这样, 相反, 您的代码会检查它是否在 WEBMODE 或 TEST 中运行, 在测试中它会初始化上下文, 否则它只会使用正常的上下文 。

这将初始化您的上下文, 您就可以从主方法运行它 。

编辑:从

  Properties props = new Properties();

  props.setProperty("java.naming.factory.initial", 
                    "com.sun.enterprise.naming.SerialInitContextFactory");

  props.setProperty("java.naming.factory.url.pkgs", 
                    "com.sun.enterprise.naming");

  props.setProperty("java.naming.factory.state",
                    "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");


  // optional.  Defaults to localhost.  Only needed if web server is running 
  // on a different host than the appserver    
  props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");

  // optional.  Defaults to 3700.  Only needed if target orb port is not 3700.
  props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

  InitialContext ic = new InitialContext(props);

当您使用主方法运行时,您不将其作为网络应用程序执行,因此您没有网络应用程序上下文。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签