1

This question has been asked a lot here, but i still can't fix my problem: I put mysql-connector-java-5.1.18-bin into C:\Program Files\Java\jre6\lib\ext folder. I have this code:

    // Load the database driver
    Class.forName("com.mysql.jdbc.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( rs.next() )
        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 ) {
    e.printStackTrace();
} 

and i get a ClassNotFoundException with the following stack trace:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at pack.Test.main(Test.java:14)

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

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
nikos
  • 2,893
  • 11
  • 30
  • 39
  • Are you using Eclipse for this? Are you writing a batch or shell script? – iowatiger08 Jan 05 '12 at 16:23
  • @iowatiger08: I am using NetBeans – nikos Jan 05 '12 at 16:26
  • 1
    Where have you put the mysql-connector jar file? Is it on the class path? – mcfinnigan Jan 05 '12 at 16:27
  • @mcfinnigan: I put it in `C:\Program Files\Java\jre6\lib\ext\mysql-connector-java-5.1.18-bin` – nikos Jan 05 '12 at 16:34
  • @nikos is that a folder or a compressed file? Either way, have you added the mysql-connector.jar to your Netbeans project build path? – mcfinnigan Jan 05 '12 at 16:35
  • @mcfinnigan i put it to `NetBeans/MyProject/Build` but it's still not working – nikos Jan 05 '12 at 16:43
  • @nikos - you can't just drop the whole mysql folder into the JRE ext lib folder, you have to get the mysql-connector-java-5.1.18.jar file and place THAT in `C:\Program Files\Java\jre6\lib\ext`. – Perception Jan 05 '12 at 16:51
  • @Perception: that's exactly what i did, i placed only the file mysql-connector-java-5.1.18.jar into `C:\Program Files\Java\jre6\lib\ext`. is there anything else i should do after that step? – nikos Jan 05 '12 at 16:54
  • 1
    Shouldn't have to - unless NetBeans is not using that JRE. Click your project and go to Properties->Libraries->Manage Platforms. Check the JDK it is using as default and make sure the mysql jar shows up under Classes. – Perception Jan 05 '12 at 17:00
  • i did that and it's fine now, thank you! – nikos Jan 05 '12 at 17:02

6 Answers6

3

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.

yankee
  • 38,872
  • 15
  • 103
  • 162
2

I think the problem is you are putting mysql-connector-java-5.1.18-bin file rather you have to put "mysql-connector-java-5.0.5.jar" file there.

i have made a web application in eclipse(IDE). so if you are making a web application on eclipse(IDE) then you can put "mysql-connector-java-5.0.5.jar" in the lib folder. below is the path where i put my "mysql-connector-java-5.0.5.jar" file...

C:\Users\Abhishek\workspace\MyProject\WebContent\WEB-INF\lib\mysql-connector-java-5.0.5.jar

1

The CLASSPATH variable can't be just the directory, it has to have the actual jar name.

karakuricoder
  • 1,065
  • 8
  • 8
0

I encountered the same problem and what I did to solve it:

  1. Downloaded the .jar accordingly to the server used: For mySQL I downloaded mysql-connector-java-5.1.18-bin.jar

  2. Added CLASSPATH in SYSTEM VARIABLES with the whole name of the .jar used. If you already have CLASSPATH var then add in it, as in PATH ";Path to Jar/jar file name" something like: CLASSPATH = d:\requisites\mySQL_connector\mysql-connector-java-5.1.18-bin.jar

  3. In Eclipse project: Right click on project->Build Path-.Configure build path. Add external JARs and add the jar file. When you succesfully do this step it will appear on the project in "Reference Libraries"

  4. An important step(if you use Android Development Tools framework as I am using) is to add also that JAR file in libs folder of your app. I did this by copy-paste the jar from my path(d:\requisites\mySQL_connector) to libs folder of my project.

Restarted the project and it worked on second run.

Lucian
  • 874
  • 11
  • 33
0

Try this

If you create the Dynamic Java Project(In Eclipse)

  1. Check the your build path for respective project

    • Step 1. Right click on your Project and Select Properties

    • Step 2. On the right side you will get the Deployment Assembly

    • Step 3. Click On the add and select Archives from File system

    • Step 4. Click on the again add and select the your respective database jar files.

Dmitriy
  • 5,525
  • 12
  • 25
  • 38
0

a bat file would look like

@echo off
set CLASSPATH=program.jar;mysql-connector-java-5.1.18-bin.jar

:start

start javaw.exe MainClass

program.jar (would contain the MainClass class as the main class) and the mysql.jar would have to reside in the same directory.

For Eclipse you have to set your project Build Path to include the mysql...jar.

iowatiger08
  • 1,892
  • 24
  • 30
  • 1
    In NetBeans, open the Project properties and select Libraries. You will then be able to "Add Project" "Add Library "Add JAR..." choose Add jar and browse to the jar file to add to your project build. http://netbeans.org/kb/docs/java/project-setup.html#projects-classpath – iowatiger08 Jan 05 '12 at 16:56