TOMCAT6 JSP MSSQL 连接报错情况以及解决方法
报错情况1: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 TestDB is not bound in this Context。
如果这里的配置对了,可能还会报其他错误,但至少不会报这个错了。
注意:<res-ref-name>JDBC/TestDB</res-ref-name> 该配置是大小写敏感的。大小写一定要完全匹配。