nikos nikos - 1 year ago 63
Java Question

ClassNotFoundException: com.mysql.jdbc.Driver

This question has been asked a lot here, but i still can't fix my problem:
I put

C:\Program Files\Java\jre6\lib\ext
I have this code:

// Load the database driver
// Get a connection to the database
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/mysql", "root", "4958ps");
// Get a statement from the connection
Statement stmt = conn.createStatement() ;
// Execute the query
ResultSet rs = stmt.executeQuery( "SELECT * FROM Cust" ) ;
// Loop through the result set
while( )
System.out.println( rs.getString(1) ) ;
// Close the result set, statement and the connection
rs.close() ;
stmt.close() ;
conn.close() ;
} catch( SQLException se ) {
System.out.println( "SQL Exception:" ) ;
// Loop through the SQL Exceptions
while( se != null ) {
System.out.println( "State : " + se.getSQLState() ) ;
System.out.println( "Message: " + se.getMessage() ) ;
System.out.println( "Error : " + se.getErrorCode() ) ;
se = se.getNextException() ;
} catch( Exception e ) {

and i get a ClassNotFoundException with the following stack trace:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(
at pack.Test.main(

I also changed
variable to be
C:\Program Files\Java\jre6\lib\ext\mysql-connector-java-5.1.18-bin

Any ideas?

Answer Source

The zip you downloaded should contain a file named mysql-connector-java-5.0.8-bin.jar or similar. Version might be different. Check that it is present and check that it is directly in your classpath (not in a subdirectory etc.).

If that's alright, check that your classpath variable is not overwritten by something else. If you execute your program manually do it like this: java -cp "C:\Program Files\Java\jre6\lib\ext\mysql-connector-java-5.0.8-bin.jar:." YourClass

If you are using an IDE such as eclipse, check your build-path settings there. (In eclipse you would right click your project -> Properties -> Java Build Path -> Add external JARs and then supply the path of your connector...jar.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download