Java和MYSQL服务器之间的连接
问题描述:
我知道这个问题已经讨论了很多次,但我无法在我的Java应用程序和MySQL服务器之间建立连接。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
端口
貌似http://*.com/questions/2121829/java-db-communications-link-failure完全相同的副本 – Barend 2012-03-31 07:25:11