0

I am getting the error java.lang.ClassNotFoundException: com.mysql.jdbc.Driver even though I have added the my-sql-connector to the build path and it is also present in classpath.

I have searched other similar questions on stack overflow most of which suggests adding the jar to classpath which I have already done. What could be other possible problem ?

build path

DBUtil.java

package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class DBUtil {
    public static Connection getMySqlConnection() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/NotesStore", "root", "123");
        //System.out.println("Connection returned from dbutil");
        return con;
    }

    public static void cleanUp(Statement st, Connection con) {
        try {
            if (st != null) {
                st.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}

.classpath

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/java-8-oracle">
        <attributes>
            <attribute name="owner.project.facets" value="java"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.server.core.container/org.eclipse.jst.server.tomcat.runtimeTarget/Apache Tomcat v8.5">
        <attributes>
            <attribute name="owner.project.facets" value="jst.web"/>
        </attributes>
    </classpathentry>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
    <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
    <classpathentry kind="lib" path="/home/diksha/Downloads/mysql-connector-java-8.0.11/mysql-connector-java-8.0.11.jar"/>
    <classpathentry kind="output" path="build/classes"/>
</classpath>
  • 2
    How are you running your tool? The *build* path is only used when you build your project, or when you run it using eclipse. And sorry: if your **runtime** class path would be correct, you would not run into his problem. – GhostCat Jul 02 '18 at 06:57
  • @GhostCat runtime classpath was the real problem.Thanks! – Diksha Manchanda Jul 02 '18 at 07:27

1 Answers1

-1

Please try to replace "com.mysql.jdbc.Driver" by "com.mysql.cj.jdbc.Driver" because you use the version 8.0 of the driver.