Results 1 to 15 of 15
  1. #1
    fish309 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Unhappy How to configure Java mysql driver and CLASSPATH?

    Hello,

    I'm using this code to test out the jdbc mysql driver:
    --------------
    import java.sql.Connection;
    import java.sql.DriverManager;

    public class Test {
    public static void main(String[] args) {
    Class.forName("com.mysql.jdbc.Driver");
    }
    }

    I've put the mysqljdbc.jar in the appropriate folder and set the CLASSPATH as well to .;C:\Program Files\Java\jre6\lib\ext\mysqljdbc.jar;C:\Program Files\Java\jre6\lib\ext\QTJava.zip

    When I compile I get a ClassNotFoundException.

    What am I doing wrong??

    Thanks, any help is greatly appreciated.

  2. #2
    gafa's Avatar
    gafa is offline Senior Member
    Join Date
    Jun 2010
    Posts
    107
    Rep Power
    0

    Default

    Do you have code errors?

    Show us the stack trace

  3. #3
    fish309 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Hi gafa, thanks for the reply.

    as requested,

    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.j ava:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at advjavaassignment.Main.main(Main.java:15)


    thanks again.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    What jar file is the file: com.mysql.jdbc.Driver.class in?
    How are you issuing the java command? Show the command line.

    Show the classpath setting by opening a command prompt and Entering: Set
    Then copy the contents here by:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    Last edited by Norm; 07-19-2010 at 08:26 PM.

  5. #5
    fish309 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    the class is in the jar file mysqljdbc.jar

    CLASSPATH=.;C:\Program Files\Java\jre6\lib\ext\mysqljdbc.jar;C:\Program Files\Java\jre6\lib\ext\QTJava.zip

    The code I'm using is as is shown above, I'm just trying to connect to the database since I'm going to be working on a data driven app.

    Thanks for the reply and for the patience, i'm new to java as you may have noticed.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    And the command line you used?

  7. #7
    fish309 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    javac Test.java using windows command prompt.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    javac is the compiler. It does not execute the class file that it creates.
    Can you COPY and PASTE here the contents of the command prompt window?
    Don't type it in or edit it, we need to see the FULL contents.

  9. #9
    fish309 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    I:\Java\Assignment\Other Stuff>javac Test.java
    Test.java:8: unreported exception java.lang.ClassNotFoundException; must be caught or declared to be
    thrown
    Class.forName("com.mysql.jdbc.Driver");
    ^
    1 error

    I:\Java\Assignment\Other Stuff>

  10. #10
    fish309 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    the stacktrace i showed you earlier was from netbeans as i tried the same code there and it autogenerated the try catch.

    thanks.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Is the problem solved now?

  12. #12
    fish309 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    no, still the same compilation error

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,518
    Rep Power
    25

    Default

    Add a try{}catch block around the code that can throw the exception

  14. #14
    fish309 is offline Member
    Join Date
    Jul 2010
    Posts
    8
    Rep Power
    0

    Default

    Hi again norm, after some digging about and trial & error I may have found something, however I'm a bit confused.

    When I use this code as below
    import java.sql.Connection;
    import java.sql.DriverManager;

    public class Test {
    public static void main(String args[]) {
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("Driver Loaded");
    }
    }

    I get this result when attempting to compile:

    I:\Java\Assignment\Other Stuff>javac Test.java
    Test.java:9: unreported exception java.lang.ClassNotFoundException; must be caug
    ht or declared to be thrown
    Class.forName("com.mysql.jdbc.Driver");
    ^
    1 error

    I:\Java\Assignment\Other Stuff>

    ==============================

    When I use this code :

    import java.sql.Connection;
    import java.sql.DriverManager;

    public class Test {
    public static void main(String args[]) {
    try {
    Class.forName("com.mysql.jdbc.Driver");
    System.out.println("Driver Loaded");
    } catch(Exception e) {
    e.printStackTrace( System.err);
    }
    }
    }

    It compiles fine and runs fine :


    I:\Java\Assignment\Other Stuff>javac Test.java

    I:\Java\Assignment\Other Stuff>java Test
    Driver Loaded

    I:\Java\Assignment\Other Stuff>

    =============================

    How and why does the try catch affect compilation of this code?

    Why is it throwing a class not found exception ONLY when not using a try catch?

    I'm a bit confused , though glad that there seems to be a solution.

    This seems to be a common issue for many beginners.

    Thanks again.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Um, you probably ought to go through some more basic Java stuff if you don't know about Exceptions and Exception handling.

    Have a tutorial.

    Put simply it's not throwing the exception. The compiler (exceptions are thrown at runtime, not compile time) is telling you that Class.forName() throws a checked exception, which means you have to handle it. It won't allow the code to compile without you handling the exception.

Similar Threads

  1. configure hibernate with mysql in eclipse galileo
    By surya.bhavanasi in forum Eclipse
    Replies: 0
    Last Post: 07-07-2010, 06:23 AM
  2. Problems with MySQL Driver
    By islan in forum JDBC
    Replies: 7
    Last Post: 08-06-2009, 04:47 PM
  3. Configure Classpath
    By Doctor Cactus in forum New To Java
    Replies: 3
    Last Post: 01-08-2009, 01:11 PM
  4. can't register a MySQL driver
    By prfalco in forum New To Java
    Replies: 4
    Last Post: 02-03-2008, 11:13 PM
  5. mysql driver problem
    By mokingsu in forum JDBC
    Replies: 4
    Last Post: 01-17-2008, 05:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •