2012-05-23 28 views
0

下面的代碼從給定用戶電子郵件的數據庫中提取照片的名稱。當我直接運行幫助程序類時,代碼會引發異常

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方法

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進行此項開發

回答

0

當您使用main方法運行它時,您並未將其作爲Web應用程序執行,因此您沒有Web應用程序上下文。

3

當您運行此爲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或測試,並在測試的情況下,將初始化上下文,否則它將只是使用普通的上下文中運行將檢查。

這將初始化您的上下文,並且您將能夠從main方法運行它。從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); 
採取Glassfish的配置:

編輯

相關問題