javax.naming.NameNotFoundException: Name jdbc is not bound in this Context
这个错误网上也搜出一大堆。各种答案都有。最广泛的答案就是配置问题,然后给出所有的配置。
经过我的摸索,终于知道是下面一段配置导致这个错误:
web工程下的,WEB-INF下的web.xml添加这一段:
<resource-ref>
<description>DB Connection</description>
<res-ref-name>JDBC/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
相对应的jsp或者java代码应该为:
DataSource ds = (DataSource) ctx.lookup("java:comp/env/JDBC/TestDB");
或者
Context envCtx = (Context) ctx.lookup("java:comp/env");
DataSource ds = (DataSource) ctx.lookup("JDBC/TestDB");
不配置,或者配置错误,就会出现:Name jdbc is not bound in this Context,如果配置成 <res-ref-name>JDBC/Test222DB</res-ref-name>,则会报Name jdbc is not bound in this Context。
如果这里的配置对了,可能还会报其他错误,但至少不会报这个错了。
Name jdbc is not bound in this Context
最新推荐文章于 2022-01-11 19:10:31 发布