Results 1 to 6 of 6
  1. #1
    pranjul137 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default connection my mysql

    i am try to connect to mysql in myeclipse by using mysql-connector-java-5.1.12.tar

    this is the code:


    import java.sql.*;

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

    try
    {
    String userName = "root";
    String password = "";
    String url = "jdbc:mysql://localhost/java";
    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 */ }
    }
    }
    }
    }

    But each time i find error "Cannot connect to database server"

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    312
    Rep Power
    10

    Default

    Why not print the stack trace of the exception getting caught to see what is really going wrong?

  3. #3
    pranjul137 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    ok i am trying...

  4. #4
    pranjul137 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    i am really new to this...if you please tell the code of it ....your help will be highly appreciated

  5. #5
    pranjul137 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

    Default

    using this code:


    import java.sql.*;

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

    try
    {
    String userName = "root";
    String password = "";
    String url = "jdbc:mysql://localhost/java";
    Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    conn = DriverManager.getConnection (url, userName, password);
    System.out.println ("Database connection established");
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    finally
    {
    if (conn != null)
    {
    try
    {
    conn.close ();
    System.out.println ("Database connection terminated");
    }
    catch (Exception e) { /* ignore close errors */ }
    }
    }
    }
    }

    got this error:
    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 Connect.main(Connect.java:15)

  6. #6
    pranjul137 is offline Member
    Join Date
    Apr 2010
    Posts
    13
    Rep Power
    0

Similar Threads

  1. TWO IPs FOR MY MYSQL CONNECTION
    By tugalsan in forum JDBC
    Replies: 2
    Last Post: 03-21-2010, 09:46 PM
  2. Java-mysql connection
    By Kligham in forum New To Java
    Replies: 16
    Last Post: 11-25-2009, 02:28 PM
  3. java mysql connection
    By sysout in forum New To Java
    Replies: 5
    Last Post: 10-31-2009, 11:48 AM
  4. Replies: 1
    Last Post: 10-29-2008, 06:38 PM
  5. no mysql connection
    By scchia in forum New To Java
    Replies: 12
    Last Post: 07-19-2008, 08:57 PM

Posting Permissions

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