开发者

I can't load the JDBC driver for MySQL

开发者 https://www.devze.com 2023-03-09 04:32 出处:网络
I\'ve been trying to load the JDBC MySQL connector with the following code: import java.sql.*; public class dbTest{

I've been trying to load the JDBC MySQL connector with the following code:

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:

java.lang.ClassNotFoundException
    at edu.rice.cs.plt.reflect.PathClassLoader.findClass(PathClassLoader.java:148)
at java.lang.ClassLoader.loadClass(Un开发者_如何学运维known 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. 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

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.

Any help would be greatly appreciated.


I have added the path to the driver (mysql-connector-java-3.1.14-bin.jar) to my classpath

The exception tells you that you didn't do it correctly.

How are you setting CLASSPATH? If it's an environment variable, you're going to learn that IDEs and app servers ignore it. Don't use it.

Don't put it in the /ext directory of your Java JDK, either.

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.
  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.
  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. If you're using a servlet/JSP engine like Tomcat 6, put it in the Tomcat /lib directory.


On IntelliJ this is how I solved this problem:

File > Project Structure > Libraries > +

Locate the jdbc connector. For me it was on C:\Users\MyName.InteliJIdea13\config\jdbc-drivers


There are two classpaths in java. Build path and run path. Build path is used when compiling .java files into .class 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. Java is slightly different because the compiled .class definitions get loaded by the jvm as they are needed.

What the net out of this is that you have to supply a runtime classpath to the 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.

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).

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. Below is how to set the run path in eclipse,netbeans, and intellij.

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消