Java - 使用JDBC连接到在线MySQL数据库时遇到问题

JDS*_*JDS 6 java mysql database jdbc

我正在尝试使用JDBC在我的Java应用程序中建立连接以访问我的在线数据库,以便我可以插入和查询表.这是我目前正在尝试的:(实际的IP /用户/传递编辑,但他们是正确的,因为我从PHP脚本做了类似的)

String url = "jdbc:mysql://984.168.199.70/my_db_name"; 
    String user = "my_username"; 
    String pass = "my_password";

Class.forName ("com.mysql.jdbc.Driver").newInstance ();
    Connection conn = (Connection) DriverManager.getConnection(url, user, pass);
    stmt = (Statement) conn.createStatement();
Run Code Online (Sandbox Code Playgroud)

但这不起作用,我收到错误:

Exception in thread "main" 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.
Run Code Online (Sandbox Code Playgroud)

之后错误日志很长.

所以要指出:在使用PHP脚本之前我已连接到此服务器,并且我已使用JDBC连接到我的localhost MySQL数据库并与之交互.

谢谢你的帮助.

小智 2

我也遇到过这个问题,而且它总是(就我而言)归因于不正确的jdbc url,例如缺少/错误的端口号(我知道您的示例代码已被编辑,但它没有指定任何端口)或不正确的IP地址,检查 mysql 是否正在运行并接受您期望的端口和 IP 上的连接失败。检查 my.cnf 中的绑定地址与 jdbc url 中的 IP 地址