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

jdbc, mysql, tomcat

Solution

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.

Problem

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: - pom.xml dependency - Tomcat LIB folder - 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!!!

Original source

Related problems