我知道这个问题已被讨论过很多次,但我无法在我的 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
您有解决办法吗?
最佳答案
尝试提供数据库的 IP-address
而不是它的名称。
说
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/*******","*******","top_secret_password");
并确保您的防火墙不会阻止 3306
端口
关于java - Java与MYSQL服务器的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9953901/