0

I am trying to connect to MYSQL server hosted on MAMP Pro. I am trying to connect from the same client machine using java and VBA. VBA connects fine but java gives me the error after few seconds

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

On Java

          String userName = "user";
          String password = "pass";
          String url = "jdbc:mysql://10.0.1.1/datab";
          Class.forName ("com.mysql.jdbc.Driver").newInstance ();
          conn = DriverManager.getConnection (url, userName, password);

On VBA

 Sub ConnectToDatabase()
        Set oConn = New ADODB.Connection
        oConn.Open "DRIVER={MySQL ODBC 3.51 Driver};" & _
            "SERVER=10.0.1.1;" & _
            "DATABASE=datab;" & _
            "USER=user;" & _
            "PASSWORD=pass;" & _
            "PORT=3306;" & _
            "Option=3"
      End Sub

telnet 10.0.1.1 3306 accepts connection from the client machine. My Bind address is the server IP on my.conf

I'm using mysql-connector-java 5.1.18
Ank
  • 6,040
  • 22
  • 67
  • 100
  • Possible duplicate: http://stackoverflow.com/questions/2983248/com-mysql-jdbc-exceptions-jdbc4-communicationsexception-communications-link-fai – jdigital May 07 '13 at 02:19

1 Answers1

1

Try this for you Java DB URL:

jdbc:mysql://localhost:3306/datab

You verify that this does not work. Examine you class path and make sure the MySql driver is on you class path.

Your code should throw an exception here:

Class.forName("com.mysql.jdbc.driver").newInstance();

To get the error, alter your code like this:

try {
   Class.forName ("com.mysql.jdbc.Driver").newInstance ();
   conn = DriverManager.getConnection (url, userName, password);
}
catch (Exception e) {
  System.out.print(e.printStackTrace());
}

This will print the error in the Eclipse console so you can cut and paste it here.

CBass
  • 983
  • 6
  • 11
  • I think the port by default is 3306 but will still try – Ank May 07 '13 at 02:12
  • I that doesn't fix it, can you post a stack trace on the error? – CBass May 07 '13 at 02:13
  • My suspicion then is that the driver is not on your class path. VBA is connecting through an ODBC driver so success there has no bearing. Make sure the driver is on your class path. – CBass May 07 '13 at 02:16
  • It probably is.. cuz eclipse isn't giving me any errors... If the driver isn't in class path the import statements appear underlined red.. – Ank May 07 '13 at 02:21
  • how do I get the stack trace from a jar? – Ank May 07 '13 at 02:22
  • I was already doing catch (Exception e) { System.err.println (e); } – Ank May 07 '13 at 18:56
  • You need to cut and paste the stack trace results into your question above so that we can look at the exact error that is occurring and tell you how to fix it. – CBass May 07 '13 at 21:11
  • I fixed it.. Don't know why it happened though.. I changed the IP address from 10.0.1.2 to 10.0.1.1 in eclipse, saved it and created a jar.. but the jar still was pointing to the earlier IP address.. I then deleted the project, created a fresh project and created a fresh jar and it worked !! – Ank May 07 '13 at 22:16