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)
6
  • 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. Commented Sep 3, 2012 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. Commented Sep 3, 2012 at 13:40
  • You should learn more about the classpath. vafer.org/blog/20081203024812 Commented Sep 3, 2012 at 13:43
  • Please post the exception trace. Commented Sep 3, 2012 at 13:46
  • edited to include stack trace. Commented Sep 3, 2012 at 13:53

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

6 Comments

Hi Resh32, I tried this approach already and it did not work!
Check this and make sure it is not a class path order problem: stackoverflow.com/questions/1585811/…
I have already looked at this question and tried the solution offered it did not work :)
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.
What Java version are you using? Could you please post the result of java -version?
|
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,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.