views:

694

answers:

5

This question might have asked here number of times . After doing some google search for the above error and doing some update, I can't understand why I'm still getting that error. I've already put my driver-- mysql-connector-java-5.1.5-bin in the classpath:

Java_Home\jre\lib\
Java_Home\jre\lib\ext\
Java_Home\lib

and the code which I'm using to connect to mysql database is:

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

Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mail","root","");

Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select message_body from deadletter");
String dbtime;
while (rs.next()) {
dbtime = rs.getString(1);
System.out.println(dbtime);
} 

con.close();

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

    }

and the complete stacktrace of the above exception is:

java.lang.ClassNotFoundException: com.mysql.jdbc:Driver
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:307)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
    at java.lang.Class.forName0(Native Method)  
    at java.lang.Class.forName(Class.java:169)
    at mail.main(mail.java:114)

Now, what's wrong I'm doing here?

+4  A: 

The most common cause is that you have some conflict in where your classes are loaded from. For example if you have 2 locations and one has JDBC drivers and the other one not then if your classloader loads from the 1st location and some class from the 1st location wants to use the driver - the driver is not there. So look for the duplicate JARs that are using your driver

DroidIn.net
You're damn correct! Thankyou!
Dusk
Glad you've been able to solve it. Stuff like that can be very frustrating
DroidIn.net
A: 

I keep the mysql-connector jar with my project rather than in Javahome. As a result, you can be sure it can be found by being sure its in the local classpath. A big upside is that you you can more the project to another machine and not have to worry about (or forget) to set this up again. I personally like including it in version control.

Willfulwizard
+2  A: 

What did you put exactly in lib, jre/lib or jre/lib/ext? Was it the jar mysql-connector-java-5.1.5-bin.jar or something else (like a directory)?

By the way, I wouldn't put it in lib, jre/lib or jre/lib/ext, there are other ways to add a jar to the classpath. You can do that by adding it explicitly the CLASSPATH environment variable. Or you can use the -cp option of java. But this is another story.

Pascal Thivent
+1 for this - no code should ever be added to these directories.
duffymo
@duffymo Agreed, even the endorsed mechanism is a mess.
Pascal Thivent
A: 

set the path to C:\Program Files\MySQL\MySQL Tools for 5.0\java\lib\mysql-connector-java-5.0.4-bin.jar

Rhea
A: 

The problem is that the catalina code apparently calls

ClassLoader.loadClass()

try this you can resolve your problem..

s.k.sumaprakash