2016-04-26 367 views
0

我在這個網站上發現了很多關於解決問題的問題和解答,但找不到解決方案,因此我終於不得不再次發佈這個相同的問題!javax.naming.NameNotFoundException | JNDI查找

我創建一個簡單的servlet DBCPDataSourceExample,並得到來自DataSource性質,而我在的Apache Tomcat V8應用服務器的server.xml & context.xml提供了一個連接。

以下爲DBCPDataSourceExample的servlet:

import javax.sql.DataSource; 
import javax.naming.Context; 
import javax.naming.InitialContext; 
import javax.naming.NamingException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class DBCPDataSourceExample extends HttpServlet { 

protected void doGet(HttpServletRequest request, HttpServletResponse respone) throws IOException { 
    Connection connection = null; 
    PreparedStatement statement = null; 
    ResultSet resultSet = null; 
    Context ctx ; 
    try { 
     try { 
      ctx = new InitialContext(); 
      Context ic= (Context) ctx.lookup("java:/comp/env"); 
      DataSource dts = (DataSource) ic.lookup("jdbc/harsh"); 
      connection = dts.getConnection(); 
     } catch (NamingException e) { 
      e.printStackTrace(); 
     } 
     statement = connection.prepareStatement("select * from stories"); 
     resultSet = statement.executeQuery(); 
     while (resultSet.next()) { 
      System.out.println("storyId: " + resultSet.getString("storyId")); 
      System.out.println("storyTitle: " + resultSet.getString("storyTitle")); 
     } 
    } catch (SQLException e) { 
     e.printStackTrace(); 
    } finally { 
     if (resultSet != null) try { resultSet.close(); } catch (SQLException e) {e.printStackTrace();} 
     if (statement != null) try { statement.close(); } catch (SQLException e) {e.printStackTrace();} 
     if (connection != null) try { connection.close(); } catch (SQLException e) {e.printStackTrace();} 
    } 

} 
} 

server.xmlResource映射:

<GlobalNamingResources> 
<!-- Editable user database that can also be used by 
    UserDatabaseRealm to authenticate users 
--> 
<Resource name="UserDatabase" auth="Container" 
      type="org.apache.catalina.UserDatabase" 
      description="User database that can be updated and saved" 
      factory="org.apache.catalina.users.MemoryUserDatabaseFactory" 
      pathname="conf/tomcat-users.xml" /> 

<Resource name="jdbc/MyDB" 
      global="jdbc/MyDB" 
      auth="Container" 
      type="javax.sql.DataSource" 
      driverClassName="com.mysql.jdbc.Driver" 
      url="jdbc:mysql://localhost/storyBoard" 
      username="root" 
      password="" 

      maxActive="100" 
      maxIdle="20" 
      minIdle="5" 
      maxWait="10000"/> 
</GlobalNamingResources> 

context.xmlResource映射:

<Resource name="jdbc/harsh" global="jdbc/MyDB" auth="container" type="javax.sql.DataSource" /> 

如果xml代碼有問題,我無法辨認。請幫忙!

下面是從控制檯堆棧跟蹤:

javax.naming.NameNotFoundException: Name [jdbc/harsh] is not bound in this Context. Unable to find [jdbc]. 
at org.apache.naming.NamingContext.lookup(NamingContext.java:818) 
at org.apache.naming.NamingContext.lookup(NamingContext.java:166) 
at com.dbcp.DBCPDataSourceExample.doGet(DBCPDataSourceExample.java:31) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:618) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:725) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) 
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) 
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) 
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086) 
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659) 
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) 
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
at java.lang.Thread.run(Unknown Source) 

的代碼行堆棧跟蹤點是這樣的:

   DataSource dts = (DataSource) ic.lookup("jdbc/harsh"); 
+0

添加完整的堆棧跟蹤請 – Jens

+0

我hv增加了堆棧跟蹤。請檢查。 –

+0

是否嘗試將「jdbc/harsh」更改爲此「jdbc/MyDB」 –

回答

1

我們的平均時間,我們需要增加資源,在server.xml中創建一個資源鏈接,它是context.xml的必要位置。

在我們的例子中,我們保持的context.xml在我們的戰爭的META-INF和樣本項外觀

<ResourceLink name="email.host.name" global="email.host.name" type="java.lang.String"/>

請驗證您的context.xml條目。

還要確保你已經把mysql驅動放在tomcat的lib目錄中。

+0

嗯,我沒有把'context.xml'放在我的應用程序的META-INF文件夾中。現在的錯誤是: 'java.sql.SQLException:無法爲連接URL創建類''的JDBC驅動程序'null'' at line: 'connection = dts。getConnection();' 現在我想它是要求連接的詳細信息在server.xml中 –

+0

您的context.xml包含如下條目, ' ' – logztechstuff

+0

是的。我可以在context.xml中提供連接屬性,並且應該可以工作。但我的問題是,如果我不想在context.xml中提供這些屬性,如何從這裏訪問server.xml中定義的屬性? –

0

在你的Server.xml文件中,我認爲你需要在url中提供端口號。 例如url =「jdbc:mysql:// localhost:3306/storyBoard」

+0

我試過並得到這個錯誤: 'java.sql.SQLException:無法創建類''的JDBC驅動程序的連接URL'null'' –

+0

請確保您已添加jdbc驅動程序與您的項目 –