com.mysql.jdbc.Driver 在构建路径中找不到 mysql 连接器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12920265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 15:12:29  来源:igfitidea点击:

com.mysql.jdbc.Driver not found with mysql connector in buildpath

mysqljdbcmysql-connector

提问by Murat Say?lgan

I use Eclipse juno. I have mysql-connector-java-5.1.22-bin.jar in my buildpath and i try to establish a DB connection to my mysql database. Here is my code

我使用 Eclipse 朱诺。我的构建路径中有 mysql-connector-java-5.1.22-bin.jar,我尝试建立到我的 mysql 数据库的数据库连接。这是我的代码

public Connection getConnectionToDB() {
    try {
        Class.forName(driver);
        Connection con = DriverManager.getConnection(url + dbName,
                userName, password);
        return con;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

When i debug it i found out the problem is at:

当我调试它时,我发现问题出在:

Class.forName(driver);

i also tried replacing it with:

我也尝试将其替换为:

Class.forName(driver).newInstance();

At this line, it breaks out to the exception and throws this exception:

在这一行,它跳出异常并抛出此异常:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1676)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1521)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at main.jdbc.BaseJdbc.getConnectionToDB(BaseJdbc.java:18)
at main.jdbc.UserDriverJdbc.queryUserById(UserDriverJdbc.java:15)
at main.drivers.UserDriver.findUserById(UserDriver.java:50)
at main.drivers.UserDriver.isLoginValid(UserDriver.java:56)
at main.controllers.LoginController.doPost(LoginController.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:164)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.__invoke(StandardHostValve.java:164)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)

回答by Murat Say?lgan

The answer was quite tricky for me to find but i figure it out. It was ofcourse classpath problem.

我很难找到答案,但我想通了。这当然是类路径问题。

If you ever encounter a problem like this and try to solve it inside eclipse, go to

如果您遇到过这样的问题并尝试在 eclipse 中解决它,请转到

eclipse -> window -> preferences -> java -> buildpath -> classpath

eclipse -> 窗口 -> 首选项 -> java -> 构建路径 -> 类路径

and add the

并添加

"mysql-connector-java-5.1.22-bin.jar"

“mysql-connector-java-5.1.22-bin.jar”

as new variable. Name it whatever you like. Hope this helps.

作为新变量。随便命名。希望这可以帮助。

回答by Gene Parmesan

You need to make sure you have the proper libraries and jar files loaded in your project. I dealt with this a lot when working with postgresql. I would think you're just missing the jar file.

您需要确保在项目中加载了正确的库和 jar 文件。在使用 postgresql 时,我经常处理这个问题。我认为您只是缺少 jar 文件。

回答by beplaya

This was my problem which was answered here: SO Link

这是我的问题,在这里得到了回答: SO Link

"If this code works in your J2SE it means you need to have a JAR file somewhere containing com.mysql.jdbc.Driver class (so called JDBC driver). This JAR needs to be visible in Tomcat. So, I would suggest to place mysql-jdbc.jar at physical location to /WEB-INF/lib directory of your project." From

“如果此代码在您的 J2SE 中工作,则意味着您需要在某处包含 com.mysql.jdbc.Driver 类(所谓的 JDBC 驱动程序)的 JAR 文件。此 JAR 需要在 Tomcat 中可见。因此,我建议将mysql-jdbc.jar 位于项目的 /WEB-INF/lib 目录的物理位置。” 从

回答by Esteban Subissi

I've been looking for a solution everywhere. Maybe this is not your solution, but using Eclipse i was exporting as a JAR file and got same exception, but then exported as "Runnable JAR"and it worked.

我一直在到处寻找解决方案。也许这不是您的解决方案,但是使用 Eclipse 我导出为 JAR 文件并得到相同的异常,但随后导出为“可运行 JAR”并且它工作正常。

回答by m.rahman

If you use netbeans then do the followings: go to your application>>>libraries>>add jar (by right click)>> "mysql-connector-java-5.1.22-bin.jar"

如果您使用 netbeans,请执行以下操作:转到您的应用程序>>>库>>添加 jar(通过右键单击)>>“mysql-connector-java-5.1.22-bin.jar”