Java和MYSQL服务器之间的连接

时间:2012-03-31 07:20:03

标签: java mysql connection

我知道这个问题已经讨论了很多次但是我无法在我的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

你有什么想法如何解决它?

1 个答案:

答案 0 :(得分:2)

尝试提供数据库的IP-address而不是其名称。 说

conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/*******","*******","top_secret_password");

并确保防火墙不阻止3306端口