Problem in connecting mysql with java-Reply Needed Urgently

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ananthu
    New Member
    • Sep 2007
    • 87

    Problem in connecting mysql with java-Reply Needed Urgently

    Hi

    I have done all the codings part for connecting mysql server with java application but when i try to compile,the compilation is successful and during execution i get the following message,

    Exception in thread "main" java.lang.NoCla ssDefFoundError : MysqlConnect

    Coding Part:

    import java.sql.*;
    import java.lang.*;

    public class MysqlConnect{
    public static void main(String[] args)throws SQLException {
    System.out.prin tln("MySQL Connect Example.");
    Connection conn = null;
    String url = "jdbc:mysql ://localhost:3306/";
    String dbName = "customer";
    String driver = "com.mysql.jdbc .Driver";
    String userName = "root";
    String password = "root";
    try {
    Class.forName(d river);
    conn = DriverManager.g etConnection(ur l+dbName,userNa me,pa ssword);
    System.out.prin tln("Connected to the database");
    conn.close();
    System.out.prin tln("Disconnect ed from database");
    } catch (java.lang.Clas sNotFoundExcept ion e) {
    //System.out.prin tln("Could not connect");
    e.printStackTra ce();
    }
    }
    }


    Execution steps:

    C:\PROGRA~1\Jav a\jdk1.6.0_01\b in>javac -classpath "c:\program files\java\jdk1 .6.
    0_01\jre\lib\ex t\mysql-connector-5.0.7-bin.jar" MysqlConnect.ja va

    C:\PROGRA~1\Jav a\jdk1.6.0_01\b in>java -classpath "c:\program files\java\jdk1 .6.0
    _01\jre\lib\ext \mysql-connector-5.0.7-bin.jar" MysqlConnect

    Result which i get is:

    Exception in thread "main" java.lang.NoCla ssDefFoundError : MysqlConnect

    I have specified the classpath clearly and i have the jar file of mysql driver in the location,

    "c:\program files\java\jdk1 .6.0_01\jre\lib \ext\mysql-connector-5.0.7-bin.jar"

    But still i get the same result as specified in execution part above.

    I am too tired in finding out the solution.....

    Urgent...Please help me to connect to mysql server.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Never put anything in that lib\ext directory of your jdk. Preferably don't put anything
    in any of the two ext directories; neither in the jdk nor jre locations. Put your jar
    anywhere you like and make your classpath value point at that jar.

    kind regards,

    Jos

    Comment

    • Ananthu
      New Member
      • Sep 2007
      • 87

      #3
      Hi

      I have placed the mysql driver file(jar file) outside the jdk folder. But the same error occurs when i try to execute my java program. Please guide me in connection of mysql with java.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Ananthu
        Hi

        I have placed the mysql driver file(jar file) outside the jdk folder. But the same error occurs when i try to execute my java program. Please guide me in connection of mysql with java.
        Good; look at the error message: it can't find your own class; better append the
        following to your classpath variable: ;. (that reads semicolon dot) which tells
        the jvm that it should look in the current working directory for classes too. The
        current working directory is where you stord your .class file.

        kind regards,

        Jos

        ps. please stop opening new threads on the same subject; I'll delete them.

        Comment

        • Ananthu
          New Member
          • Sep 2007
          • 87

          #5
          Hi

          Atlast with your guidance i have connected mysql with java.Thaks for your kind help and guidance. Sorry for posting the same thread again and again. Here after i will not do this. Thank you so much.

          Please keep in touch with me in this kind of troubleshooting activity as and when i require.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by Ananthu
            Hi

            Atlast with your guidance i have connected mysql with java.Thaks for your kind help and guidance. Sorry for posting the same thread again and again. Here after i will not do this. Thank you so much.

            Please keep in touch with me in this kind of troubleshooting activity as and when i require.
            You're welcome of course; your problem was not about database connections;
            it was a classpath problem (which you hopefully don't experience anymore from
            now on).

            There are plenty of people roaming around here that are very well capable of
            answering Java questions. Just ask your question here if you bump into one.

            kind regards,

            Jos

            Comment

            Working...