Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat 8 - java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://xxx/myApp'

Tags:

mysql

tomcat

jdbc

I looked everywhere on the web in order to find a solution to my problem, but I had no luck! :(

I'm trying to develop a servlet able to connect to a MySQL database (Connection Pool) and to deploy it on a Tomcat 8 server.

I have a context.xml file in META-INF like this:

<?xml version="1.0" encoding="UTF-8"?>
   <Context antiJARLocking="true" path="/DBConnectionPoolTest">
       <Resource name="jdbc/testdb"
                 auth="Container"
                 type="javax.sql.DataSource"
                 username="xxx" password="xxx"
                 driverclassname="com.mysql.jdbc.Driver"
                 url="jdbc:mysql://xxx/myApp"
                 maxactive="10"
                 maxidle="4" />
   </Context>

In WEB-INF I created the web.xml file:

<?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>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>
</web-app>

Finally, on the servlet class, I use:

Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/testdb");
...
connection = dataSource.getConnection();

But on this line, when I try to get a connection from the datasource, I get the following exception:

java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://xxx/myApp'
t org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2065)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1939)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412)
    at DBPoolConnectionServlet.processRequest(DBPoolConnectionServlet.java:73)
...
Caused by: java.sql.SQLException: No suitable driver

I thought it could be caused by the absence of the JDBC Driver, so I putted mysql-connector-java-5.1.34-bin.jar in:

  1. pom.xml dependency
  2. Tomcat LIB folder
  3. WEB-INF/lib folder inside the webapp

but without luck.

Can you please tell me what I'm doing wrong? I feel so noob... :(

Thank you very much for your time spent reading (and hopefully answering) my question!!!

like image 639
Brutus Avatar asked Feb 25 '15 17:02

Brutus


2 Answers

Looks like you are doing all fine, so:

driverClassName="com.mysql.jdbc.Driver" (the capitals may matter).

You place the mysql-connector-java-5.1.34-bin.jar (it has to have jar extension to be detected in tomcat/lib (dont put it on your webapp path it should be loaded by tomcat class loader).

If it is not helping and you are starting your webapp from IDE. Try to start tomcat form console and deploy your app manually. If you have more than one tomcat installed make sure that CATALINA_HOME is set to the one you place your mysql jar.

like image 173
Zielu Avatar answered Oct 12 '22 04:10

Zielu


Steps to fix Netbeans/Tomee/MySQL connection issues and resolve FAIL - Deployed application at context path /RA7Web-1.0-SNAPSHOT but context failed to start:

  1. Edit server.xml on your Tomee server using a full path name for appBase:

     <!-- Fixed the problem of cannot deploy by providing a full path  for appBase -->`
     <Host name="localhost"  
           appBase="C:\apache\apache-tomee-7.0.2-plume\webapps"
           unpackWARs="true" autoDeploy="true">
    
  2. Add the following to your project web-xml:

    <resource-ref>
        <description>Resource Allocation database</description>
        <res-ref-name>jdbc/resourcealloc</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    
  3. Add the following to your Context.xml:

      <Resource name="jdbc/resourcealloc"
            auth="Container"
            type="javax.sql.DataSource"
            username="root"
            password="mySecretPwd"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/resourcealloc"
            maxActive="15"
            maxIdle="3"/>
    
  4. The following resources.xml was generated, but I had to edit several back-slashes out so it matched my URL:

      <resources>
          <Resource id="jdbc/resourcealloc" type="javax.sql.DataSource">
              jdbcDriver=com.mysql.jdbc.Driver
              password=mySecretPwd
              userName=root
              jdbcUrl=jdbc:mysql://localhost:3306/resourcealloc?zeroDateTimeBehavior=convertToNull
          </Resource>
      </resources>
    
like image 37
Jim Reitz Avatar answered Oct 12 '22 04:10

Jim Reitz