简体   繁体   English

我无法加载 MySQL 的 JDBC 驱动程序

[英]I can't load the JDBC driver for MySQL

I've been trying to load the JDBC MySQL connector with the following code:我一直在尝试使用以下代码加载 JDBC MySQL 连接器:

import java.sql.*;

public class dbTest{
   public static void main(String[] args) throws SQLException, ClassNotFoundException
   {
    Class.forName("com.mysql.jdbc.Driver"); 
   }
}

And I keep getting a class not found exception:而且我不断收到 class not found 异常:

java.lang.ClassNotFoundException
    at edu.rice.cs.plt.reflect.PathClassLoader.findClass(PathClassLoader.java:148)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at dbTest.main(dbTest.java:6)

I have added the path to the driver (mysql-connector-java-3.1.14-bin.jar) to my classpath and double checked to make sure it was correct.我已将驱动程序的路径 (mysql-connector-java-3.1.14-bin.jar) 添加到我的类路径中,并仔细检查以确保它是正确的。 I also added copies of the jar to the ext folder of my Java installation based on what I read from this article: http://www.developer.com/java/data/jdbc-and-mysql-installation-and-preparation-of-mysql.html我还根据我从这篇文章中读到的内容将 jar 的副本添加到我的 Java 安装的 ext 文件夹中: Z80791B3AE7002CB88C246876D9FAA8F8F8Z://www.and-mysql-installation-and-jparation/java of-mysql.html

I also searched through posts of others who have had this problem, but all of the responses so far have been saying to add the connector jar to the classpath, which I have already done.我还搜索了其他遇到此问题的帖子,但到目前为止,所有回复都说要将连接器 jar 添加到类路径中,我已经这样做了。

Any help would be greatly appreciated.任何帮助将不胜感激。

On IntelliJ this is how I solved this problem:在 IntelliJ 上,这就是我解决此问题的方法:

File > Project Structure > Libraries > +文件 > 项目结构 > 库 > +

Locate the jdbc connector.找到 jdbc 连接器。 For me it was on C:\Users\MyName.InteliJIdea13\config\jdbc-drivers对我来说,它在 C:\Users\MyName.InteliJIdea13\config\jdbc-drivers

I have added the path to the driver (mysql-connector-java-3.1.14-bin.jar) to my classpath我已将驱动程序的路径(mysql-connector-java-3.1.14-bin.jar)添加到我的类路径中

The exception tells you that you didn't do it correctly.异常告诉您您没有正确执行此操作。

How are you setting CLASSPATH?你是如何设置 CLASSPATH 的? If it's an environment variable, you're going to learn that IDEs and app servers ignore it.如果它是一个环境变量,您将了解到 IDE 和应用服务器会忽略它。 Don't use it.不要使用它。

Don't put it in the /ext directory of your Java JDK, either.也不要将它放在 Java JDK 的 /ext 目录中。

The right way to do it depends on how you're using it:正确的方法取决于您如何使用它:

  1. If you're running inside an IDE like Eclipse or IntelliJ, you have to add the JAR to a library.如果您在 IDE 中运行,例如 Eclipse 或 IntelliJ,则必须将 JAR 添加到库中。
  2. IF you're running in a command shell, use the -p option for javac.exe when you compile and java.exe when you run.如果您在命令 shell 中运行,请在编译时对 javac.exe 使用 -p 选项,在运行时使用 java.exe 选项。
  3. If you're using it in a web app, you can start by putting it in the WEB-INF/lib directory of your WAR file.如果您在 web 应用程序中使用它,您可以首先将其放在 WAR 文件的 WEB-INF/lib 目录中。 If you're using a servlet/JSP engine like Tomcat 6, put it in the Tomcat /lib directory.如果您使用像 Tomcat 6 这样的 servlet/JSP 引擎,请将其放在 Tomcat /lib 目录中。

There are two classpaths in java. java 中有两个类路径。 Build path and run path.构建路径和运行路径。 Build path is used when compiling.java files into.class files.编译时使用构建路径将.java文件转换成.class文件。 In a language like C you have a linker stage that fills in all the missing symbols when you run the linker on a bunch of object files. In a language like C you have a linker stage that fills in all the missing symbols when you run the linker on a bunch of object files. Thats why for.exe(windows) or other native binaries(linux) there is no run path.这就是为什么 for.exe(windows) 或其他本机二进制文件(linux) 没有运行路径的原因。 Java is slightly different because the compiled.class definitions get loaded by the jvm as they are needed. Java 略有不同,因为编译后的 class 定义在需要时由 jvm 加载。

What the net out of this is that you have to supply a runtime classpath to the jvm.这样做的结果是您必须为 jvm 提供运行时类路径。 At the command line you use java.exe which searches a few places by default including $CLASSPATH, the current directory/lib, and whatever you supply to the -cp option.在命令行中,您使用 java.exe,它默认搜索几个位置,包括 $CLASSPATH、当前目录/lib 以及您提供给 -cp 选项的任何内容。

IDEs are different from the command line because they are attempting to shield you from some of the nastiness of running java.exe and supplying the locations where all the.class files are(which would be onerous on a large project). IDE 与命令行不同,因为它们试图让您免于运行 java.exe 并提供所有 .class 文件所在的位置(这在大型项目中会很麻烦)。

Most IDE's have some sort of "Run Configuration" tab that allows you to specify certain libraries or locations with classes that will be used when you run your application.大多数 IDE 都有某种“运行配置”选项卡,允许您指定某些库或位置以及运行应用程序时将使用的类。 Below is how to set the run path in eclipse,netbeans, and intellij.下面是如何在 eclipse、netbeans 和 intellij 中设置运行路径。

http://javahowto.blogspot.com/2006/06/set-classpath-in-eclipse-and-netbeans.html http://javahowto.blogspot.com/2006/06/set-classpath-in-eclipse-and-netbeans.html

http://www.jetbrains.com/idea/webhelp/run-debug-configuration-application.html http://www.jetbrains.com/idea/webhelp/run-debug-configuration-application.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM