【问题标题】:Tomcat, MySQL -> Cannot create JDBC driver of class '' for connect URL 'null'Tomcat、MySQL -> 无法为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序
【发布时间】:2016-03-26 07:44:59
【问题描述】:

我知道这看起来像是一个重复的问题,但我尝试了 stackoverflow 和其他论坛中的所有帖子,但无济于事。

Apache Tomcat 8.0.32 运行 MySQL 5.6 的 Amazon RDS 在 EC2 windows 服务器上运行 tomcat

我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
...
  <resource-ref>
    <description>datasource</description>
    <res-ref-name>jdbc/bbDataSource</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
  </resource-ref>
...
</web-app>

Context.xml 位于我的应用程序的 META-INF 文件夹中:

<?xml version="1.0" encoding="UTF-8"?>
<Context path="/ch">
   <Resource name="jdbc/bbDataSource"
        auth="Container"
        type="javax.sql.DataSource"
        username="********"
        password="********"
        driverClassName="com.mysql.jdbc.Driver"
        url="jdbc:mysql://*url*:3306/x?zeroDateTimeBehavior=convertToNull"/>
 </Context>

在 ServletContextListener 中创建数据源

try {
        dataSource = (DataSource) new InitialContext().lookup("java:comp/env/jdbc/bbDataSource");
        mLogger.log(Level.CONFIG, "Startup - AppContextServletListener - contextInitialized - Created dataSource object: {0}", dataSource);
        sce.getServletContext().setAttribute("dataSource", dataSource);
}

启动过程中没有问题。 访问 dataSource tomcat 时会引发上述错误。 jar 文件位于 tomcat 的 lib 文件夹中(我怀疑这是问题所在,因为我也在同一个应用程序中使用 hibernate,它能够毫无问题地访问数据库)。

我还注意到以下几点:$CATALINA_HOME/conf/Catalina/localhost 没有 'ch.xml' -> 尝试复制 context.xml 并在那里重命名为 'ch.xml' 但无济于事。

server.xml -> 宿主节点下

 <Host name="localhost"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">
        <Context path="/ch" docBase="ch"></Context>
        <Context path="/ch/asdasd" docBase="C:\\xasdasd\\asdasd">   </Context>

【问题讨论】:

标签: mysql tomcat jdbc driver tomcat8


【解决方案1】:

这解决了它:

在 Server.xml 中添加了上下文下的资源行 想知道为什么它需要定义两次?有什么线索吗?

 <Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true">
     <Context path="/ch" docBase="ch">
        <Resource name="jdbc/bbDataSource"
             auth="Container"
             type="javax.sql.DataSource"
             username="********"
             password="********"
             driverClassName="com.mysql.jdbc.Driver"
             url="jdbc:mysql://x:3306/x?zeroDateTimeBehavior=convertToNull"/>
     </Context>
 </Host>

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 1970-01-01
    • 2015-05-03
    • 2012-03-09
    • 1970-01-01
    • 2011-11-08
    • 2018-01-14
    • 2011-05-14
    • 2017-08-23
    相关资源
    最近更新 更多