java.lang.ClassNotFoundException:com.mysql.jdbc.Driver,即使类路径正确

时间:2018-07-02 06:54:00

标签: java mysql database eclipse jdbc

即使我将java.lang.ClassNotFoundException: com.mysql.jdbc.Driver添加到了构建路径,并且它也存在于类路径中,我仍然收到错误my-sql-connector

我在堆栈溢出中搜索了其他类似的问题,其中大多数建议将jar添加到我已经完成的classpath中。还有什么可能是其他问题?

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>

1 个答案:

答案 0 :(得分:0)

由于您使用的驱动程序版本为8.0,请尝试用"com.mysql.jdbc.Driver"替换"com.mysql.cj.jdbc.Driver"