javax.naming。NameNotFoundException:名称[jdbc/eswastha]在此上下文中不受约束。无法找到(jdbc)

时间:2023-01-19 22:03:49

I shall describe the environment first

我先描述一下环境。

environment: : Netbeans 7.2 and Tomcat 7.0.27.0 is configured with Netbeans ID

环境::Netbeans 7.2和Tomcat 7.0.27.0配置了Netbeans ID。

when i take the build separately and put it in webapps folder and run than there is no issue but when i run the application in the IDE itself i get javax.naming.NameNotFoundException: Name [jdbc/eswastha] is not bound in this Context. Unable to find [jdbc]. this exception.

当我单独把构建放到webapps文件夹中并运行时,没有问题,但是当我在IDE中运行应用程序时,我得到了javax.命名。NameNotFoundException:名称[jdbc/eswastha]在此上下文中不受约束。找不到(jdbc)。这个异常。

conf/context.xml

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*" />

and web.xml

和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>  

and the java class :

和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;

    }
}

Please help me to identify the issue.

请帮助我确认这个问题。

Regards

问候

2 个解决方案

#1


1  

Is it missing? Check

这是失踪吗?检查

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

Based on your comment : Cannot create JDBC driver of class '' for connect URL 'null'

基于您的评论:不能创建类的JDBC驱动程序“用于连接URL‘null’”


Make sure your JDBC Driver and copy driver jar file under tomcat-home/lib.

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

It will be reference for your. here

这将是你的参考。在这里

#2


1  

You may want to try to access the data source in full JNDI name, e.g.: "java:comp/env/jdbc/test" Source: http://www.coderanch.com/t/442367/Tomcat/jdbc-bound-Context

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

On second note, I noticed that you've defined jjdbc, instead of jdbc in web.xml:

在第二个注释中,我注意到您已经定义了jjdbc,而不是web.xml中的jdbc。

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

#1


1  

Is it missing? Check

这是失踪吗?检查

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

Based on your comment : Cannot create JDBC driver of class '' for connect URL 'null'

基于您的评论:不能创建类的JDBC驱动程序“用于连接URL‘null’”


Make sure your JDBC Driver and copy driver jar file under tomcat-home/lib.

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

It will be reference for your. here

这将是你的参考。在这里

#2


1  

You may want to try to access the data source in full JNDI name, e.g.: "java:comp/env/jdbc/test" Source: http://www.coderanch.com/t/442367/Tomcat/jdbc-bound-Context

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

On second note, I noticed that you've defined jjdbc, instead of jdbc in web.xml:

在第二个注释中,我注意到您已经定义了jjdbc,而不是web.xml中的jdbc。

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