Results 1 to 10 of 10
  1. #1
    Pragya is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default Connectivity issue-MySQL and Java

    Hi

    There is a problem in JDBC connectivity.I have downloaded the JDBC

    Connector/J for MySQL connectivity with java. I've changed the

    CLASSPATH environment variable, made changes in the code and tried

    and tested with several modifications. But it still doesn't work.

    Here is the source code.

    import java.sql.*;

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

    try
    {
    String url="jdbc:mysql://localhost/example";
    Class.forName("com.jdbc.mysql.Driver").newInstance ();
    conn=DriverManager.getConnection(url);
    System.out.println("Database connection established");
    }
    catch(Exception e)
    {
    System.out.println("Cannot connect to server");
    }
    finally
    {
    if(conn!=null)
    {
    try
    {
    conn.close();
    System.out.println("Database connection terminated");
    }
    catch(Exception e)
    { }
    }
    }
    }
    }

    It compiles and displays "Cannot connect to database" when run on the

    command prompt.

    Here are some specifications:

    (1) My JDK is in C:\ drive
    (2) MySQL is in Program Files.
    (3) The Connector/J is in C:\jdk1.6.0_16\jre\lib\ext
    The ext folder has the mysql-connector-java-5.1.10-bin.jar file.

    Hence the CLASSPATH environment variable includes :

    C:\jdk1.6.0_16\jre\lib\ext\mysql-connector-java-5.1.10-bin.jar

    Please help me with this so that I can move on...

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    364
    Rep Power
    5

    Default

    Add this line to your code
    and post all Exception here please:

    catch(Exception e)
    {
    e.printStackTrace();
    System.out.println("Cannot connect to server");
    }


    Maybe you must specify port like 3306
    jdbc:mysql://localhost:3306/mydb

  3. #3
    Pragya is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    Thanks

    In which Exception block do I make the changes?
    Making the changes you suggested within the paranthesis of the first exception and adding :3306 after local host didn't help. It still gives the same exception.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    In all of them.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    Yes. Always print a stack trace, otherwise you're simply losing all the important exception information.

  6. #6
    Pragya is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    Here's what comes on printing the stack trace :

    java.lang.ClassNotFoundException : com.jdbc.mysql.Driver
    at java.net.URLCClassLoader$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 07)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:2 52)
    at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:320)
    at java.lang.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at Check.main(Check.java:12)
    Cannot connect to database
    How do I verify whether SQL is running or not?

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,830
    Rep Power
    19

    Default

    The connector/J jar file isn't on your classpath.

  8. #8
    Pragya is offline Member
    Join Date
    Jan 2010
    Posts
    4
    Rep Power
    0

    Default

    What should the classpath be so that Connector/J is included?

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Pragya View Post
    What should the classpath be so that Connector/J is included?
    It should include the complete path to the Driver jarfile, of course.

  10. #10
    cjr9968 is offline Member
    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Default

    Try moving the jar into your project directory.
    If you're using netbeans you can just drag the .jar file under the "Libraries" folder in your project. That worked for me.

Similar Threads

  1. MySQL connectivity (hours wasted)
    By ribbs2521 in forum New To Java
    Replies: 13
    Last Post: 10-21-2009, 07:18 AM
  2. mysql query performance issue
    By gilbertsavier in forum JDBC
    Replies: 0
    Last Post: 08-05-2009, 10:34 AM
  3. Replies: 0
    Last Post: 10-22-2008, 08:50 PM
  4. MySQL + Java - auto incrementation issue
    By int80 in forum Advanced Java
    Replies: 6
    Last Post: 07-29-2008, 06:04 PM
  5. MySQL issue: Exception while getting MetaDataInfo
    By sandeepspatil in forum JDBC
    Replies: 2
    Last Post: 07-27-2007, 06:54 AM

Posting Permissions

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