我在這個網站上發現了很多關於解決問題的問題和解答,但找不到解決方案,因此我終於不得不再次發佈這個相同的問題!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.xml
Resource
映射:
<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.xml
Resource
映射:
<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");
添加完整的堆棧跟蹤請 – Jens
我hv增加了堆棧跟蹤。請檢查。 –
是否嘗試將「jdbc/harsh」更改爲此「jdbc/MyDB」 –