Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Cannot connect to test database in java with jdbc

Options
  • 24-10-2007 7:39pm
    #1
    Closed Accounts Posts: 192 ✭✭


    I got this off the link below
    http://www.stardeveloper.com/articles/display.html?article=2003090201&page=4

    I have set CLASSPATH to C:\Program Files\Java\jre1.6.0_03\lib\ext\'mysql-connector-java-5.1.5-bin.jar

    I am using Jcreator IDE



    package com.stardeveloper.example;

    import java.sql.*;

    public class JdbcExample1 {

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

    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection("jdbc:mysql:///test", "root", "secret");

    if(!con.isClosed())
    System.out.println("Successfully connected to MySQL server...");

    }
    catch(Exception e) {
    e.printStackTrace();
    }
    finally {
    try {
    if(con != null)
    con.close();
    } catch(SQLException e) {}
    }
    }
    }

    It compiles fine but gives the exception below;

    Configuration: <Default>
    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:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at com.stardeveloper.example.JdbcExample1.main(JdbcExample1.java:11)

    Process completed.

    Please help:confused::(


Comments

  • Registered Users Posts: 1,127 ✭✭✭smcelhinney


    Its definitely a classpath error. How are you running the program? Have you tried a command line compile and run??

    I've never used JCreator, only eclipse and netbeans, but this is definitely because the runtime processor cannot find the required library (the Connector/J one).


  • Registered Users Posts: 303 ✭✭brian_rbk


    have you tried copying the mysql-connector-java-3.0.17-ga-bin.jar file into the lib folder of your project. I'm using netbeans, and it worked as soon as i did that.


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    jtiernan wrote: »
    I got this off the link below
    http://www.stardeveloper.com/articles/display.html?article=2003090201&page=4

    I have set CLASSPATH to C:\Program Files\Java\jre1.6.0_03\lib\ext\'mysql-connector-java-5.1.5-bin.jar

    I am using Jcreator IDE



    package com.stardeveloper.example;

    import java.sql.*;

    public class JdbcExample1 {

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

    try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection("jdbc:mysql:///test", "root", "secret");

    if(!con.isClosed())
    System.out.println("Successfully connected to MySQL server...");

    }
    catch(Exception e) {
    e.printStackTrace();
    }
    finally {
    try {
    if(con != null)
    con.close();
    } catch(SQLException e) {}
    }
    }
    }

    It compiles fine but gives the exception below;

    Configuration: <Default>
    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:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at com.stardeveloper.example.JdbcExample1.main(JdbcExample1.java:11)

    Process completed.

    Please help:confused::(

    I am coming from Eclipse here so and I haven't used JCreator myself.

    I take it you are using the JCreator IDE to compile and run the program rather than the command line? If so, more than likely it doesn't use the system classpath when it is compilling and running programs, but probably has it own internal classpath somewhere..

    If you have a look around in the menu for Properties or settings, I am sure you will find something like Classpath / Build Path. If you add the jar in here it should work just fine.


Advertisement