简体   繁体   English

无法使用META-INF / context.xml文件连接Servlet JDBC连接池中的数据库

[英]unable to connect database in Servlet JDBC Connection Pool using META-INF/context.xml file

I am trying to connect mysql database using Servlet using context.xml file. 我正在尝试使用context.xml文件使用Servlet连接mysql数据库。 By referring example provided on website: 通过参考网站上提供的示例:

http://viralpatel.net/blogs/2009/09/database-connection-pooling-tomcat-eclipse-db.html http://viralpatel.net/blogs/2009/09/database-connection-pooling-tomcat-eclipse-db.html

But I am getting exception: javax.naming.NameNotFoundException: jdbc not bound 但是我收到异常:javax.naming.NameNotFoundException:jdbc未绑定

after that I have added tag in web.xml, then also it didn't work and gave another exception as: 之后,我在web.xml中添加了标记,然后它也没有起作用,并给出了另一个异常,例如:

Error during deploy; 部署期间发生错误; - nested throwable: (javax.naming.NamingException: resource-ref: jdbc/mytest has no valid JNDI binding. Check the jboss-web/resource-ref.) -嵌套的throwable:(javax.naming.NamingException:resource-ref:jdbc / mytest没有有效的JNDI绑定。请检查jboss-web / resource-ref。)

(Note:mytest is my database name) (注意:mytest是我的数据库名称)

additional details: I have added my configuration detail for refernce context.xml: 其他详细信息:我为引用 context.xml 添加了我的配置详细信息

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <!-- Specify a JDBC datasource -->
    <Resource name="jdbc/mytest" auth="Container"
        type="javax.sql.DataSource" username="root" password="mysql"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://localhost:3306/mytest"
        maxActive="10" maxIdle="4" />
</Context>

web.xml: web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

  <display-name>testjdbc</display-name>
<resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/mytest</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref> 

  <servlet>
    <description></description>
    <display-name>TestServlet</display-name>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>com.mytest.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>TestServlet</servlet-name>
    <url-pattern>/TestServlet</url-pattern>
  </servlet-mapping>

</web-app>

servlet.java : servlet.java:

public void init() throws ServletException 
 {
        try 
        {
            // Get DataSource
            Context initContext  = new InitialContext();
            Context envContext  = (Context)initContext.lookup("java:/jdbc/mytest");
            dataSource = (DataSource)envContext.lookup("jdbc/mytest");

        } 
         catch (NamingException e) 
         {
            e.printStackTrace();
        }
    }

The tutorial you mentioned is for the Tomcat but you are now using jboss-web. 您提到的教程是针对Tomcat的,但是您现在正在使用jboss-web。

Each server may have slightly different way to configure the JNDI Datasource .For the jboss-web , you can refer to this . 每个服务器配置JNDI数据源的方式可能略有不同。有关jboss-web的信息,请参考this

When the Datasource resource is defined with name="jdbc:testdb" in context.xml, you have to look up with that same name in your InitialContext.lookUp() method. 当在context.xml中使用name =“ jdbc:testdb”定义数据源资源时,您必须在InitialContext.lookUp()方法中使用相同的名称进行查找。 The databse name (mytest) shall be given in the connection url. 数据库名称(mytest)应在连接URL中给出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Maven WebApp META-INF context.xml - Maven WebApp META-INF context.xml 可以从某些属性文件中读取Tomcat中的Web / Meta-Inf / Context.xml - can Web/Meta-Inf/Context.xml read in Tomcat from some properties file 如何在java war文件中的META-INF / context.xml内的Tomcat 8上指定“上下文路径”? - How can I specify “Context path” on Tomcat 8 inside META-INF/context.xml in java war file? 无法将META-INF / context.xml重新加载到../conf/Catalina/localhost/myapp.xml - reloading META-INF/context.xml to ../conf/Catalina/localhost/myapp.xml not working Tomcat中context.xml的正确META-INF目录位置是什么? - What is the correct META-INF directory location for context.xml in Tomcat? 如何在Maven的包生成期间选择一个/META-INF/context.xml? - How can I chose one /META-INF/context.xml during the Maven's package generation? 部署战争。 Tomcat 不考虑上下文。考虑到 META-INF 中的 xml - Deploying War. Tomcat not taking context.xml from META-INF in consideration META-INF / context.xml导致我的Java Webapp出现问题 - META-INF/context.xml causing problems in my Java Webapp Maven-war-plugin覆盖删除/META-INF/context.xml - maven-war-plugin overlay removes /META-INF/context.xml 如何在开发和生产环境中使用不同的META-INF / context.xml文件 - How to Use Different META-INF/context.xml Files For Development and Production Environments
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM