Results 1 to 5 of 5
  1. #1
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default MySQL JDBC Driver ClassNotFound

    Hi,

    I have problem to make a connection to mySQL db

    Here's the code in NetBeans 7

    public class TestConnection {
    public static void main(String[] argv) throws Exception
    {
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");

    if (!con.isClosed())
    {
    System.out.println("Connected");
    con.close();
    }
    else {System.out.println("Not connected");}


    }
    }

    I'm using XP n I have set the classpath in the system environment variable to the folder that hold the jar for msql connection.

    However I get this error message :
    Exception in thread "main" 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.j ava:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
    at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at TestConnection.main(TestConnection.java:14)

    I've browsed the topics n see many things but mostly about classpath which I already set, so now in quite a confusion.
    Anyway I intended to first check the connection to MySQL before anything else which I never do before.

    I also use javac from command line with including -cp <path to mysql connection jar file> file.java....then run java file, also generating around the same error message


    Thanks in advance
    Last edited by Levian; 06-17-2011 at 07:28 AM.

  2. #2
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    Changing the code into :

    public static void main (String[] args)
    {
    Connection conn = null;

    try
    {
    String userName = "root";
    String password = "root";
    String url = "jdbc:mysql://localhost:3306/test";
    Class.forName ("com.mysql.jdbc.Driver").newInstance();
    conn = DriverManager.getConnection (url, userName, password);
    System.out.println ("Database connection established");
    }
    catch (Exception e)
    {
    System.err.println ("Cannot connect to database server");
    System.out.println (e);
    }
    finally
    {
    if (conn != null)
    {
    try
    {
    conn.close ();
    System.out.println ("Database connection terminated");
    }
    catch (Exception e) { /* ignore close errors */ }
    }
    }
    }

    Just see from one post that seems working n here's the debug in netbeans

    debug:
    Cannot connect to database server
    java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    BUILD SUCCESSFUL (total time: 1 second)

    I've set the library in Tools - Libraries...adding the path to mysql connector jar file n even specifically point to the jar file itself.

  3. #3
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    11

    Default

    You are not adding MySQL library to your project. To add this using netbeans
    Right click your project, you can see it in project window.
    Then, click properties>libraries>Add Library>Import> select MySQL JDBC Library.

  4. #4
    Levian is offline Senior Member
    Join Date
    Jun 2011
    Posts
    109
    Rep Power
    0

    Default

    That solve the problem with netbeans but I'm curious as to how that's done with command line if you know.

    Anyway the problem is solved, thanks a lot

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    27

    Default

    With the command line you use the -cp option to add classpaths.
    So:
    java -cp<path to jars>;. myclass
    Don't forget the "."

Similar Threads

  1. ClassNotFoundException: com.mysql.jdbc.Driver
    By tBKwtWS in forum New To Java
    Replies: 9
    Last Post: 06-15-2011, 07:01 PM
  2. com.mysql.jdbc.Driver Exception !
    By RealSilhouette in forum New To Java
    Replies: 6
    Last Post: 06-10-2011, 09:30 AM
  3. classnotfound exception in jdbc
    By tariq9112003 in forum Advanced Java
    Replies: 5
    Last Post: 06-07-2011, 10:47 AM
  4. com.mysql.jdbc.Driver
    By uthpalaw in forum Eclipse
    Replies: 2
    Last Post: 10-14-2010, 05:09 AM
  5. Replies: 4
    Last Post: 03-31-2010, 12:08 PM

Posting Permissions

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