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