【发布时间】:2018-12-10 08:22:32
【问题描述】:
即使我已将 my-sql-connector 添加到构建路径并且它也存在于类路径中,但我收到错误 java.lang.ClassNotFoundException: com.mysql.jdbc.Driver。
我已经搜索了关于堆栈溢出的其他类似问题,其中大多数建议将 jar 添加到我已经完成的类路径中。还有什么其他可能的问题?
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>
【问题讨论】:
-
你是如何运行你的工具的? build 路径仅在您构建项目或使用 eclipse 运行它时使用。抱歉:如果你的 runtime 类路径是正确的,你就不会遇到他的问题。
-
@GhostCat 运行时类路径是真正的问题。谢谢!
标签: java mysql database eclipse jdbc