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(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. 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.
This error occurs if JDBC is not able to find a suitable driver for the URL format passed to the getConnection() method e.g. "jdbc:mysql://" in our case. In order to solve this error, you need the MySQL JDBC driver like mysql-connector-java-5.1. 36. jar in your classpath.
Connection errors are typically caused by incorrect or invalid connection string parameters, such as an invalid host name or database name. Verify that you are using correct and valid connection string parameters for the Gate. Jdbc. Url property.
Use a web browser to visit the MySQL download site and locate the source and binary download link for the archive format that you want to use (typically zip for Microsoft Windows systems or a gzipped tarball for Linux systems). Click that link to initiate the download process. Registration may be required.
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:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With