unable to connect to mysql server

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhitam30111985
    New Member
    • Aug 2007
    • 112

    unable to connect to mysql server

    hi all .. java/jdbc newbie here ... i cant seem to perform the most basic step for database connectivity using java . here is my code :

    [CODE=java]

    import java.sql.*;

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

    try
    {
    String userName = "myname";
    String password = "mypassword ";
    String url = "jdbc:mysql:loc alhost";
    Class.forName ("com.mysql.jdb c.Driver").newI nstance ();
    conn = DriverManager.g etConnection (url, userName, password);
    System.out.prin tln ("Database connection established");
    }
    catch (Exception e)
    {
    System.err.prin tln ("Cannot connect to database server");
    }
    finally
    {
    if (conn != null)
    {
    try
    {
    conn.close ();
    System.out.prin tln ("Database connection terminated");
    }
    catch (Exception e) { /* ignore close errors */ }
    }
    }
    }
    }

    [/CODE]

    when i am connecting to the same server using some other programming language such as python it is working perfectly fine .
    what i cant figure out is what to assign to the "url" variable.. in other words .. how shall i determine the url of the company mysql server ? every time i run this code i get the same error "Cannot connect to database server".

    I am using debian linux OS

    any help appreciated,
    regards
    rhitam.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Replace [CODE=java]System.err.prin tln ("Cannot connect to database server");[/CODE] on line 20 in your code with

    [CODE=java]e.printStackTra ce()[/CODE]

    That way you can see more information about the exception that is being thrown.

    Comment

    • rhitam30111985
      New Member
      • Aug 2007
      • 112

      #3
      hi r035198x

      i did as u said .. got the following errors :

      Code:
      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 connect.main(connect.java:15)
      cant figure what it means

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by rhitam30111985
        hi r035198x

        i did as u said .. got the following errors :

        Code:
        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 connect.main(connect.java:15)
        cant figure what it means
        That means the MySQL driver classes cannot be found by your program. You should add the MySQL connector jar file's path to your program's classpath.

        Comment

        • rhitam30111985
          New Member
          • Aug 2007
          • 112

          #5
          uh.. how exactly do i do that ?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by rhitam30111985
            uh.. how exactly do i do that ?
            Do you have the MySQL driver .jar file with you?

            Comment

            • rhitam30111985
              New Member
              • Aug 2007
              • 112

              #7
              i have already installed the jdbc driver for mysql.. namely

              libmysql-java

              but when i search for it using the following command :

              whereis libmysql-java

              it shows blank

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by rhitam30111985
                i have already installed the jdbc driver for mysql.. namely

                libmysql-java

                but when i search for it using the following command :

                whereis libmysql-java

                it shows blank
                Download the connector .jar from here and add it to your classpath.

                Comment

                • rhitam30111985
                  New Member
                  • Aug 2007
                  • 112

                  #9
                  ok i downloaded and extracted the following file :

                  mysql-connector-java-3.0.17-ga.tar.gz

                  now by adding it to classpath do u mean just copy and paste it there? in that case how do i determine what is the classpath?

                  thanks

                  Comment

                  • ajos
                    Contributor
                    • Aug 2007
                    • 283

                    #10
                    Originally posted by rhitam30111985
                    ok i downloaded and extracted the following file :

                    mysql-connector-java-3.0.17-ga.tar.gz

                    now by adding it to classpath do u mean just copy and paste it there? in that case how do i determine what is the classpath?

                    thanks
                    Now that you have got the mysql-connector-java-3.0.17-ga.tar.gz link just unzip the file, it contains a mysql-connector-java-3.0.17--bin which is a jar file, put the copy of the jar file in the lib folder of the application and include the path of the jar file in the class path. For more information read the mysql connector docs, it has everything that you need.
                    regards,
                    ajos

                    Comment

                    • rhitam30111985
                      New Member
                      • Aug 2007
                      • 112

                      #11
                      Hi all , sorry for making this topic active again but still facing problems ...

                      to add the jar file to classpath i did this :

                      downloaded the jar file and unipped it and placed in the following folder :

                      /home/rhitam/java/myprograms/

                      now to add to class path .. i have to first change the accessibilty of this jar file ... .

                      chmod +x /home/rhitam/java/myprograms/mysql-connector-java-3.0.17-ga-bin.jar

                      this worked ..
                      then .. i did this ...
                      set CLASSPATH=.;/home/rhitabrata/java/myprograms/mysql-connector-java-3.0.17-ga-bin.jar

                      it gave the error:


                      -bash: /home/rhitabrata/java/myprograms/mysql- connector-java-3.0.17-ga-bin.jar: cannot execute binary file


                      the code is also located in the same location as the jar file ... ( /home/rhitam/java/myprograms/)

                      what to do ?

                      Comment

                      Working...