Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Connectivity problem:MySQL and Java

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Connectivity problem: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...
    Last edited by Json; January 22nd, 2010 at 03:54 AM. Reason: Please use code tags.


  2. #2
    Junior Member
    Join Date
    Jan 2010
    Location
    Orpington, Kent, UK
    Posts
    18
    Thanks
    0
    Thanked 9 Times in 8 Posts

    Default Re: Connectivity problem:MySQL and Java

    Well obviously an Exception is being thrown so it will display "Cannot connect to server".
    presumably not "Cannot connect to database" as you specified.

    As you do not seem to be printing out the Exception in the code I would suggest you do the following,

    change line

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

    to

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

    and post what the exception is, that will point you in the right direction.

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Connectivity problem:MySQL and Java

    Yeah could you please do a printStackTrace for us?

    Is your database actually up and running?

    // Json

Similar Threads

  1. Testing the DataSource with MySQL
    By srinivasan_253642 in forum JDBC & Databases
    Replies: 0
    Last Post: January 9th, 2010, 02:23 AM
  2. access database connectivity from outside an application
    By suchirag in forum JDBC & Databases
    Replies: 0
    Last Post: October 29th, 2009, 03:03 AM
  3. Database connectivity problem-Oracle
    By Entrant in forum JDBC & Databases
    Replies: 3
    Last Post: October 11th, 2009, 11:08 AM
  4. Adding Marathi words to MySQL table
    By vaishali in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: July 8th, 2009, 07:43 AM
  5. Use of Unicode 5.1 in MySQL DB with Java
    By Desert Fox in forum JDBC & Databases
    Replies: 2
    Last Post: November 12th, 2008, 08:29 AM