10 Replies Latest reply: Nov 2, 2008 6:28 AM by 843785 RSS

    connecting to mysql

    843785
      i have runed this code but i get:

      Cannot connect to database server


      is there anything else that i must chang on my setings for this to run?
       import java.sql.*;
      
         public class HelloWorld
         {
             public static void main (String[] args)
             {
                 Connection conn = null;
      
                 try
                 {
                     String userName = "myuser";
                     String password = "mypass";
                     String url = "jdbc:mysql://localhost/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");
                 }
                 finally
                 {
                     if (conn != null)
                     {
                         try
                         {
                             conn.close ();
                             System.out.println ("Database connection terminated");
                         }
                         catch (Exception e) { /* ignore close errors */ }
                     }
                 }
             }
         }
        • 1. Re: connecting to mysql
          843785
          Replace this line:
          System.err.println ("Cannot connect to database server");
          with this one:
          e.printStackTrace()
          (or leave the original line and add the other one).

          This way you will get a lot more information about what exception was thrown and where it occured.
          • 2. Re: connecting to mysql
            843785
            java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
                 at java.net.URLClassLoader$1.run(Unknown Source)
                 at java.security.AccessController.doPrivileged(Native Method)
                 at java.net.URLClassLoader.findClass(Unknown Source)
                 at java.lang.ClassLoader.loadClass(Unknown Source)
                 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
                 at java.lang.ClassLoader.loadClass(Unknown Source)
                 at java.lang.ClassLoader.loadClassInternal(Unknown Source)
                 at java.lang.Class.forName0(Native Method)
                 at java.lang.Class.forName(Unknown Source)
                 at HelloWorld.main(HelloWorld.java:14)
            • 3. Re: connecting to mysql
              843785
              alin19 wrote:
              java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
              So, did you google for ClassNotFoundException? What could it mean? When is it thrown?
              • 4. Re: connecting to mysql
                843785
                This means you don't have the mysql jar in your class path.
                • 5. Re: connecting to mysql
                  843785
                  thanks for your patience,

                  can you tell me how to add that jar?

                  and i have searched for this error, but until now i haven't found anything that helped me fixed this, i will find it on my own if i search a little more, but i thought that you could help me faster

                  10x

                  Edited by: alin19 on Nov 2, 2008 3:34 AM
                  • 6. Re: connecting to mysql
                    843785
                    alin19 wrote:
                    10x
                    Please don't use SMS speak. In this case it's only a minor annoyance, but it makes your post a lot harder to read, especially for those who don't have English as their primary language.

                    Do you know about [the Classpath] (read here if you are using Linux or Solaris)?

                    It's where Java finds its classes. You should learn about it.

                    We could give you the simple solution, but it won't help a lot if you don't understand what it does.
                    • 7. Re: connecting to mysql
                      843785
                      thanks, i'm new in java as you have noticed

                      i have used once classpath but i was using javac for compiling, now i'm using eclipse and windows

                      thanks again for you help it is still beter then nothing
                      • 8. Re: connecting to mysql
                        843785
                        i have downloaded this mysql-conector:

                        http://www.java2s.com/Code/Jar/JDBC-Drivers/Downloadmysqlconnectorjava513rcbinjar.htm


                        add it to the folder where my code is located , and in the c:\program files\java\bin and in c:\program files\java\lib

                        and still nothing
                        • 9. Re: connecting to mysql
                          843785
                          alin19 wrote:
                          i have downloaded this mysql-conector:

                          http://www.java2s.com/Code/Jar/JDBC-Drivers/Downloadmysqlconnectorjava513rcbinjar.htm
                          Ok, that part is correct. But next time you should probably download it from MySQL directly: [http://www.mysql.com/products/connector/j/].
                          add it to the folder where my code is located , and in the c:\program files\java\bin and in c:\program files\java\lib
                          Now you are just guessing what to do. That's not good, you should find out what is wrong and how to correct it. Read [http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html].

                          If you use Eclipse, the Classpath still has to be defined, but it works differently (see "Java Build Path" on your project properties).
                          • 10. Re: connecting to mysql
                            843785
                            i've solve it :)

                            thanks