javax.naming.NameNotFoundException : Name [jdbc/eswastha] is not bound in this Context. 无法找到 [jdbc]

标签 java tomcat7 jndi

我先描述一下环境

环境::使用 Netbeans ID 配置 Netbeans 7.2 和 Tomcat 7.0.27.0

当我单独进行构建并将其放入 webapps 文件夹中并运行时,没有任何问题,但是当我在 IDE 本身中运行应用程序时,我得到 javax.naming.NameNotFoundException:名称 [jdbc/eswastha] 在此上下文中未绑定(bind)。无法找到[jdbc]。此异常。

conf/context.xml

<Resource name="jdbc/eswastha" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               url="jdbc:mysql://localhost:3306/eswastha"
               driverClassName="com.mysql.jdbc.Driver"
               username="root" password="r14@17*" />

和 web.xml

<resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jjdbc/eswastha</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>  

和java类:

import java.sql.Connection;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class JDBCManager {

    public Connection mysqlConnection()  {
        Connection dbConnection = null;
        try {

            Context initContext = new InitialContext();
            Context envContext  = (Context)initContext.lookup("java:/comp/env");
            DataSource ds = (DataSource)envContext.lookup("jdbc/eswastha");
            dbConnection = ds.getConnection();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return dbConnection;

    }
}

请帮我找出问题所在。

问候

最佳答案

是不是不见了? 检查

 <res-ref-name>jjdbc/eswastha</res-ref-name>
 and
 <Resource name="jdbc/eswastha"..../>

根据您的评论: 无法为连接 URL“null”创建类“”的 JDBC 驱动程序


确保您有 JDBC 驱动程序,并将驱动程序 jar 文件复制到 tomcat-home/lib 下。

供您引用。 here

关于javax.naming.NameNotFoundException : Name [jdbc/eswastha] is not bound in this Context. 无法找到 [jdbc],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12928030/

相关文章:

java - 调试时的 Jboss 错误 - native 方法中的 fatal error : JDWP No transports initialized, jvmtiError=AGENT_ERROR_TRANSPORT_INIT(197)

java - Cassandra 2.1.13 支持哪些 Java 版本

spring - 在 Spring 中 Autowiring JNDI 资源

java - 如何为DataStore设置jndi.properties?

java - 从给定的数字字符串生成 16 位唯一的十六进制值

java - CountDiv (Codility) 挑战算法的性能问题

java - 使用 ActiveMQ Ajax 客户端时出现长时间延迟

java - slf4j 版本与 Tomcat 和 WebApplicationInitializer 不匹配

java - ${catalina.base}/lib 子目录下的属性文件解析

exception - 在 Tomcat 7 中使用不同的 ROOT 应用程序时 JNDI 资源不可用