0

我正在尝试连接到MYSQL托管在MAMP Pro. 我正在尝试使用 java 和VBA. VBA连接正常但java在几秒钟后给了我错误

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.

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);

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接受来自客户端机器的连接。我的绑定地址是服务器 IPmy.conf

I'm using mysql-connector-java 5.1.18
4

1 回答 1

1

为您尝试这个 Java DB URL:

jdbc:mysql://localhost:3306/datab

您确认这不起作用。检查您的类路径并确保 MySql 驱动程序在您的类路径上。

您的代码应在此处引发异常:

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

要获得错误,请像这样更改您的代码:

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

这将在 Eclipse 控制台中打印错误,以便您可以在此处剪切并粘贴它。

于 2013-05-07T02:11:31.107 回答