Arjun
Arjun

Reputation: 3803

MySql Driver Class Autoloading

Configuration

jdk version : 1.8.0_60-b27

project : maven jsp project

Mysql connector : mysql-connector-java-5.1.38 (Copied to Tomcat's lib)

Pom.xml

 <!--MySql Connector/J Dependency-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
            <scope>provided</scope>
        </dependency>

Code Causing Error :

try(Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/myDatabase","root","")){
            connection=con;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

Error log

java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/myDatabase
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at util.DBConnectivity.getDBConnection(DBConnectivity.java:16)
    at controller.MyController.doGet(MyController.java:25)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2503)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2492)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)

I tried both of the scopes [provided and compile] in pom.xml but no change in the output!!!

Tried hundred times but can't figure out why project is not able to find the driver????

Upvotes: 3

Views: 606

Answers (2)

Gergely Bacso
Gergely Bacso

Reputation: 14661

What you need to do is just these two steps, and not more:

  1. Include your dependency properly:

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

Notice that scope is not configures, so by default it will be compile, which will make it available at runtime too.

The combination of manually copying this file to WEB-INF\lib then redeploy with scope=provided will not work because the folder will get recreated. In case you would like to make this jar available only at runtime, you need to place it to Tomcat\lib.

  1. You need to manually load the driver before you attempt to connect anywhere. Make sure you do this before using DriverManager.

    Class.forName("com.mysql.jdbc.Driver");

Upvotes: 2

Paizo
Paizo

Reputation: 4204

you are not getting the connection from a datasource hence you are not using the mysql driver in the tomcat lib.

Using DriverManager you need to load the jdbc library yourself using Class.forName("com.mysql.jdbc.Driver").newInstance(); and by removing the provided entry for it in the pom.xml or... use a datasource :)

Upvotes: -1

Related Questions