【问题标题】:javax.naming.NameNotFoundException: Name [jdbc/eswastha] is not bound in this Context. Unable to find [jdbc]javax.naming.NameNotFoundException:名称 [jdbc/eswastha] 未绑定在此上下文中。找不到 [jdbc]
【发布时间】:2012-10-07 08:09:21
【问题描述】:

我先描述一下环境

环境:Netbeans 7.2 and Tomcat 7.0.27.0 is configured with Netbeans ID

当我单独进行构建并将其放入 webapps 文件夹并运行时,没有问题,但是当我在 IDE 本身中运行应用程序时,我得到 javax.naming.NameNotFoundException: Name [jdbc/eswastha] is not bound in this Context. Unable to find [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;

    }
}

请帮我找出问题。

问候

【问题讨论】:

标签: java tomcat7 jndi


【解决方案1】:

它不见了吗? 检查

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

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


确保您的JDBC Driver 并在tomcat-home/lib 下复制驱动程序jar 文件。

这将是您的参考。 here

【讨论】:

  • 谢谢,有拼写错误,但现在我得到了这个org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null' 异常
  • 我也包含了所需的库
【解决方案2】:

您可能想尝试以完整的 JNDI 名称访问数据源,例如:“java:comp/env/jdbc/test” 来源:http://www.coderanch.com/t/442367/Tomcat/jdbc-bound-Context

第二点,我注意到您在 web.xml 中定义了 jjdbc,而不是 jdbc

<res-ref-name>jjdbc/eswastha</res-ref-name>

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
  • 注明。会记住并在以后的所有帖子中这样做。
猜你喜欢
  • 2018-11-02
  • 2017-07-17
  • 1970-01-01
  • 2011-09-22
  • 2012-09-11
  • 2016-12-08
  • 2018-09-12
  • 2019-06-16
  • 1970-01-01
相关资源
最近更新 更多