mysqltomcatintellij-ideaweb.xmlcontext.xml

Error Message: Unable to get connection, DataSource invalid: "java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'"


I'm currently using the following technology stack: intellj 14.1.5, Spring MVC, tomcat 8.0.28, and mysql workbench 6.3. I'm trying to connect to my database but get an error. I feel like the issue is with my context.xml file. Any help is appreciated.

What my context.xml file looks like:

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

What I added to my 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>

What my jsp file looks like:

<%@ 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>

I also have the following dependency in my pom.xml file:

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

Solution

  • I was able to fix my issue. It seems that I needed to create a META-INF directory within the webapp directory. Once I did that I put the context.xml file within the META-INF directory and it corrected the issue.