gpt4 book ai didi

java - 当我直接运行帮助程序类时,代码抛出异常

转载 作者:行者123 更新时间:2023-12-02 00:22:18 24 4
gpt4 key购买 nike

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

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("suhailgupta03@gmail.com");
%>

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

但是如果我通过添加 main 方法单独使用此类

enter image description here

在该帮助程序类中,它抛出一个异常:

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

为什么会发生这种情况?我正在使用 glassfish 服务器和 netbeans 进行此开发。

最佳答案

当您将其作为 Web 应用程序运行时,您的上下文将使用服务器详细信息进行初始化

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

当您通过调用 main 方法作为独立程序运行时,情况并非如此

您可以通过初始化上下文来摆脱这个问题。

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 中运行,如果测试它会初始化上下文,否则它将只使用正常的上下文。

这将初始化您的上下文,您将能够从主方法运行它。

编辑:Glassfish 配置取自 here

  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);

关于java - 当我直接运行帮助程序类时,代码抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10721655/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com