0

I am attempting to connect to a mysql database, with a connection url of:

jdbc:mysql://127.0.0.1:3306/test

I have downloaded the coorect Mysql driver to connect with the database, and have tried a multitude of approaches to set the driver, with each not working. So far I have tried placing the JAR file in the following places (and changing the PATH environment variable accordingly)

JRE/LIB/
JDK/LIB/
JRE/LIB/mysql-connector-java-5.1.21
JDK/LIB/mysql-connector-java-5.1.21

The path for the JAR file has been its location + mysql-connector-java-5.1.21-bin.jar

Over the last 4+ hours I have read multiple questions and solutions on StackOverflow, as well as online tutorials about this issue, and none have solved the problem.

I have been using the following code to attempt a connection

import java.sql.*;
import java.util.Date;

public class DatabaseHelper{

    private Connection          conn                = null;
    private Statement           statement           = null;
    private PreparedStatement   preparedStatement   = null;
    private ResultSet           resultSet           = null;
    private String              url                 = null;

    public DatabaseHelper(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conn    = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test");
            System.out.println("Driver Loaded!");
        }catch(SQLException e){
            e.printStackTrace();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}

Stacktrace

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:186)
        at DatabaseHelper.<init>(DatabaseHelper.java:28)
        at DatabaseTest.<init>(DatabaseTest.java:6)
        at DatabaseTest.main(DatabaseTest.java:14)
Php Pete
  • 762
  • 3
  • 14
  • 29
  • How exactly are you executing the code? What environment and what command? That'll provide more insight into the classpath actually being used. By the way, declaring those DB resources as instance variables this way is absolutely a terribly bad idea. Don't do that. Note that this is unrelated to your concrete problem. – BalusC Sep 03 '12 at 13:37
  • Put the connector jar file in your classpath. The jdk and jre lib folders are not good places for your classpath. Create a folder under your root classpath (.) named lib and place it there. Then `java -cp .:./lib/mysql-connector-java-5.1.21.jar YourMainApp` should work. Placing other libs in the jre or jdk lib folders makes it hard to upgrade you Java version. – km1 Sep 03 '12 at 13:40
  • You should learn more about the classpath. http://vafer.org/blog/20081203024812/ – km1 Sep 03 '12 at 13:43
  • Please post the exception trace. – Santosh Sep 03 '12 at 13:46
  • edited to include stack trace. – Php Pete Sep 03 '12 at 13:53
  • km1 could you be more clear: you are suggesting I create a classpath folder named lib at C:\, and then place my JAR file for the connector therer? – Php Pete Sep 03 '12 at 13:54

2 Answers2

1

You need to add the JAR to your classpath. When launching the java app, simply put:

 java -cp mysql-connector-java-5.1.21-bin.jar TheNameOfYourMainClass
Resh32
  • 6,500
  • 3
  • 32
  • 40
  • Hi Resh32, I tried this approach already and it did not work! – Php Pete Sep 03 '12 at 13:55
  • Check this and make sure it is not a class path order problem: http://stackoverflow.com/questions/1585811/classnotfoundexception-com-mysql-jdbc-driver – Resh32 Sep 03 '12 at 13:57
  • I have already looked at this question and tried the solution offered it did not work :) – Php Pete Sep 03 '12 at 13:58
  • Also try a trivial example using the classpath above, if it fails, maybe you added other JARs in your lib directory you should not have. – Resh32 Sep 03 '12 at 13:59
  • What Java version are you using? Could you please post the result of java -version? – Resh32 Sep 03 '12 at 14:00
  • Also check that your JAR for mysql-connector is not corrupted. Try to rename it to ZIP, then expand it (A JAR is A ZIP) and make sure that the ZIP expands fine. Also check that com.mysql.jdbc.Driver is there. – Resh32 Sep 03 '12 at 14:01
0

i faced this problem before while working on a project, I put the jar file in 2 locations, what worked for me was as follows:

I put the mysql jar in JAVA_HOME/JRE_FOLDER/lib/ext/

then the other thing is that i create a libs folder inside the project (directly in the project's folder) and also put the mysql jar inside it. After that add the jar (the one inside the libs folder) to the building path of the project.

If you use Eclipse, adding the jar to the building path is done by right-clicking on the jar and choosing "Build Path" from the menu and then choosing "Add to Build Path".

Hope this helps,

Samer Makary
  • 1,815
  • 2
  • 22
  • 25