mysql - 错误消息 : Unable to get connection, 数据源无效: "java.sql.SQLException: Cannot create JDBC driver of class ' ' 对于连接 URL 'null' ”

标签 mysql tomcat intellij-idea web.xml context.xml

我目前使用以下技术堆栈:intellj 14.1.5、Spring MVC、tomcat 8.0.28 和 mysql workbench 6.3。我正在尝试连接到我的数据库,但出现错误。我觉得问题出在我的 context.xml 文件上。如有任何帮助,我们将不胜感激。

我的 context.xml 文件是什么样的:

   <!-- maxTotal: Maximum number of database connections in pool. Make sure you
        configure your mysqld max_connections large enough to handle
        all of your db connections. Set to -1 for no limit.
        -->

   <!-- maxIdle: Maximum number of idle database connections to retain in pool.
        Set to -1 for no limit.  See also the DBCP documentation on this
        and the minEvictableIdleTimeMillis configuration parameter.
        -->

   <!-- maxWaitMillis: Maximum time to wait for a database connection to become available
        in ms, in this example 10 seconds. An Exception is thrown if
        this timeout is exceeded.  Set to -1 to wait indefinitely.
        -->

   <!-- username and password: MySQL username and password for database connections  -->

   <!-- driverClassName: Class name for the old mm.mysql JDBC driver is
        org.gjt.mm.mysql.Driver - we recommend using Connector/J though.
        Class name for the official MySQL Connector/J driver is com.mysql.jdbc.Driver.
        -->

   <!-- url: The JDBC connection url for connecting to your MySQL database.
        -->

   <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
             maxTotal="100" maxIdle="30" maxWaitMillis="10000"
             username="root" password="1234" driverClassName="com.mysql.jdbc.Driver"
             url="jdbc:mysql://localhost:3306/module1"/>

我添加到 web.xml 的内容:

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

我的 jsp 文件是什么样的:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
  <html>
      <body>
      <h1>Test1</h1>

       <sql:query var="rs" dataSource="jdbc/TestDB">
             select id from myguests
        </sql:query>

       <c:forEach var="row" items="${rs.rows}">
             ID: ${row.id}<br/>
        </c:forEach>

      </body>
   </html>

我的 pom.xml 文件中还有以下依赖项:

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.37</version>
    </dependency>

最佳答案

我能够解决我的问题。看来我需要在 webapp 目录中创建一个 META-INF 目录。完成此操作后,我将 context.xml 文件放入 META-INF 目录中,它纠正了问题。

关于mysql - 错误消息 : Unable to get connection, 数据源无效: "java.sql.SQLException: Cannot create JDBC driver of class ' ' 对于连接 URL 'null' ”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33558943/

相关文章:

php - MySQL 中较慢的 UPDATE 语句

mysql - Node.js脚本通过socket.io连接MySQL数据库

java - 找不到 ObjectDB EntityManager

java - 如何在 tomcat 6 web.xml servlet-mapping servlet-pattern 中定义子路径

java - Intellij Idea 评论错误

mysql - 如何在 Yii2 框架中 ping mysql 服务器

升级mysql版本后mysql无法运行

java - Tomcat - 在 WEB-INF 中显示图像

java - 错误 : Module not specified (IntelliJ IDEA)

gradle - 如何在库jar中打包扩展功能?