我知道这个问题已经讨论了很多次但是我无法在我的Java应用程序和MySQL服务器之间建立连接。
我正在使用代码表格教程:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class JDBCTest{
private Connection conn = null;
public void connect(){
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
System.out.println("test");
conn = DriverManager.getConnection("jdbc:mysql://mysql.***.***.**:3306/******","*******","top_secret_password");
System.out.println("test2");
} catch (SQLException ex) {
// handle any errors
System.out.println("SQLException: " ex.getMessage());
System.out.println("SQLState: " ex.getSQLState());
System.out.println("VendorError: " ex.getErrorCode());
}catch(Exception e){e.printStackTrace();}
}
}
发生的错误是:
test
SQLException: 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.
SQLState: 08S01
VendorError: 0
你有什么想法如何解决它?
答案 0 :(得分:2)
尝试提供数据库的IP-address
而不是其名称。
说
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/*******","*******","top_secret_password");
并确保防火墙不阻止3306
端口