1

I have searched the internet trying to find solutions on using java and JDBC to connect to MySQL. Unfortunately I have found multiple answers and none that solved my problem.

I downloaded the JDBC from MySQL, and unzipped the file to find the .jar. I placed the .jar in my C:/Program Files (X86)/Java/JDK.../JRE/lib/ext folder. I set my environmental variable classpath (maybe CLASSPATH, ClassPath ??) to the following:

%CLASSPATH%;.;C:\Program Files (x86)\Java\jdk1.8.0_65\jre\lib\ext

I use the script I composed based on all the different solutions I have seen to get this:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class MySql {

    public static void main(String[] args)
    {
        Connection con = getConnection();
        if (con != null) {
            System.out.println("Connection Made");
        }
        else {
            System.out.println("Connection not made");
        }
    }

    private static Connection getConnection() {


        Connection con = null;  
        try {
            Class.forName("com.mysql.jdbc");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "java", "java");
            System.out.println("Connection Made");

            conn.close();

        } 
        catch (SQLException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        catch (ClassNotFoundException e)
        {
            System.out.println(e.getMessage());
            System.exit(0);
        }
        return con;
    }


 }

It compiles with javac MySql.java. Then when it runs (java MySql) I get com.mysql.jdbc. I have read that I don't need to register the driver, but when I remove Class.forName all I get is 'cannot find JDBC driver' error.

I can't narrow down my problem to either: 1). Classpath not setup correctly. 2). Improper java connection code. 3). Unable to locate MySQL server.

Any help would be appreciated.

Edit -

I placed the .jar file onto Desktop for testing purposes. Changed system classpath variable:

%CLASSPATH%;.;C:\User\User\Desktop\mysql-connector-java-5.1.38-bin.jar

Then when I add the trace for the error statement I get:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader.findClass<Unkown Source>
    at java.lang.ClassLoader.loadclass (Unknown source)
    at sun.misc.Launcher$AppClassLoader.loadclass(Unkown Source)
    etc.
    etc.

edit 2 - I have spent two days using Connect Java to a MySQL database as a resource and none of the instructions I have followed have solved my problem.

Community
  • 1
  • 1
Erich Keithly
  • 33
  • 1
  • 7
  • Could you do e.printStackTrace() in case of exception and paste full exception into your question? The message alone isn't helping too much. – Jan Dec 11 '15 at 22:31
  • Did you include the JDBC jar in your build path? – Joseph Evans Dec 11 '15 at 22:32
  • 1
    It's time to get out of the habit of throwing things into ".../jre/lib/ext" as apparently that option [will be removed in Java 9](https://blogs.oracle.com/java-platform-group/entry/planning_safe_removal_of_under). – Gord Thompson Dec 11 '15 at 22:43

5 Answers5

3

Instead of

System.out.println(e.getMessage());

Do this

e.printStackTrace();

You will see that the exception is:

java.lang.ClassNotFoundException: com.mysql.jdbc
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at MySql.getConnection(MySql.java:24)
    at MySql.main(MySql.java:10)

Remove the Class.forName. You might get an access denied or some other error but it will solve the ClassNotFoundException. Here is the final edited version that should work:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MySql {

    public static void main(String[] args)
    {
        Connection con = getConnection();
        if (con != null) {
            System.out.println("Connection Made");
        }
        else {
            System.out.println("Connection not made");
        }
    }

    private static Connection getConnection() {
        Connection con = null;  
        try {
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/sakila", "java", "java");
            System.out.println("Connection Made");
            conn.close();
        } 
        catch (SQLException e)
        {
            e.printStackTrace();
            System.exit(0);
        }
        return con;
    }
 }
Josh Chappelle
  • 1,558
  • 2
  • 15
  • 37
1

If you need Class.forName() you have to use correct class:

     Class.forName("com.mysql.jdbc.Driver");

But with JDBC 4 that has become no longer needed.

Create full Exception stack traces and use the result to search here - most common errors have been solved here before.

NoSuitableDriverFound is a very strong indication your mysql-connector.jar (not the .zip....) is missing from your classpath when you run your code.

You could try like this:

Run java -cp .;C:\User\User\Desktop\mysql-connector-java-5.1.38-bin.jar MySql

Jan
  • 13,738
  • 3
  • 30
  • 55
0

I think you must add the path including the jar file name of the driver in your classpath. Like so:

%CLASSPATH%;.;C:\Program Files (x86)\Java\jdk1.8.0_65\jre\lib\ext\mysql-connector-java-xxx.jar
Andreas Vogl
  • 1,776
  • 1
  • 14
  • 18
0

If your Java is version 6 or above you doesn't need Class.formName(...) code. And for ensure that everything works, compile and execute your code in the follow way. To compile:

java -cp PATH_TO_DRIVER; YOUR_CLASS.java

To execute:

java -cp PATH_TO_DRIVER; YOUR_CLASS

Change YOUR_CLASS to the name of your class and PATH_TO_DRIVER to the path where you download the MySQL driver.

Hope it helps!

avaz
  • 523
  • 2
  • 8
  • 20
  • You doesn't need to put the driver on this location, it can be anywhere on your system since you specify the location when you compile or run your code (or both). The way I suggested you can avoid change your classpath environment variable too. – avaz Dec 11 '15 at 22:42
  • You just try the suggestion to see if it works as way to narrow down where your problem could be. Maybe your class path configuration is wrong or maybe your code or maybe the jar that you put in that location is not the real mysql driver. The way a suggested is less error prone because you lower your dependency on external system configuration. – avaz Dec 11 '15 at 22:46
  • I'm getting a java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/sakila – Erich Keithly Dec 11 '15 at 23:10
  • You have the same code as @Jan? – avaz Dec 11 '15 at 23:26
  • Yes I removed the driver location statement. I copied and pasted from @Josh Chapelle – Erich Keithly Dec 12 '15 at 00:17
0

Try this code!

  public static void main(String[] argv) {

    try {
        Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
        System.out.println("Where is your MySQL JDBC Driver?");
        e.printStackTrace();
        return;
    }

    System.out.println("MySQL JDBC Driver Registered!");
    Connection connection = null;

    try {
        connection = DriverManager
        .getConnection("jdbc:mysql://localhost:3306/yourSCHEMAname,"login", "password");

    } catch (SQLException e) {
        System.out.println("Connection Failed! Check output console");
        e.printStackTrace();
        return;
    }

    if (connection != null) {
        System.out.println("You made it, take control your database now!");
    } else {
        System.out.println("Failed to make connection!");
    }
  }
}
Serg Shapoval
  • 707
  • 1
  • 11
  • 39