Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't load the JDBC driver for MySQL

Tags:

java

mysql

jdbc

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.

like image 558
kevinAlbs Avatar asked May 28 '11 23:05

kevinAlbs


People also ask

How do I fix no suitable driver found for JDBC MySQL?

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.

Why is JDBC not working?

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.

How do I download MySQL driver for JDBC?

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.


3 Answers

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.
like image 96
duffymo Avatar answered Oct 23 '22 01:10

duffymo


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

like image 44
Nautilus Avatar answered Oct 23 '22 01:10

Nautilus


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

like image 45
nsfyn55 Avatar answered Oct 23 '22 01:10

nsfyn55