您的位置:首页 > 其它

Name jdbc is not bound in this context

2015-09-11 11:09 507 查看
"javax.naming.NameNotFoundException: Name jdbc is not bound in this Context"

 

这个错误网络上一大堆。大概知道这是应为WEB-INF里的web.xml文件中没有以下配置或者是配置错误:

 

<resource-ref>

  <description>GuestBook</description>

  <res-ref-name>jdbc/oracleds</res-ref-name>  

  <res-type>javax.sql.DataSource</res-type>

  <res-auth>Container</res-auth>  

 </resource-ref> 

 

首先,我们在Tomcat安装文件中找到 conf/context文件并打开, 在这里,我以我用的Oracle数据库

 

来配置。

 

 <Resource name="jdbc/oracleds" auth="Container" type="javax.sql.DataSource"

 

 maxIdle="30" maxActive="10" maxWait="10000" username="scott" password="113416"

 

 driverClassName="oracle.jdbc.OracleDriver" 

 

 url="jdbc:oracle:thin:@127.0.0.1:1521:SNAIL"/>

 

而在WEB-INF文件下的 web.xml中,我们配置

 

<resource-ref></resource-ref> 的信息必须与Tomcat中的配置信息一一对应。

 

 

例如:

 

在Tomcat中配置的name="jdbc/oracleds" ,而在 web.xml文件中配置的name="jdbc/oracle"都会引起

 

javax.naming.NameNotFoundException: Name jdbc is not bound in this Context  错误提示!

认真的看了自己的context.xml和web.xml中的配置,觉得它们正确无误。而错误依然不能消除,那么你看这篇文章就对了,也许它会是你的指路明灯。

你看一下自己的JSP文件,是否有这样一句:

DataSource ds=(DataSource)initContext.lookup("java:/comp/env/jdbc/xinabai");

请将它改为:

 Context initContext = new InitialContext();   

 Context envContext = (Context)initContext.lookup("java:/comp/env");      

DataSource ds = (DataSource)envContext.lookup("jdbc/xinbai");

虽然无数本书和帖子告诉你,这两个方法相同。但是,很明显,前一种会在某种情况下产生Name jdbc is not bound in this Context的错误。

具体原因我不清楚,希望知道原因的高人可以告知一二。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: