简体   繁体   English

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException使用JDBC访问远程MySQL数据库时

[英]com.mysql.jdbc.exceptions.jdbc4.CommunicationsException when using JDBC to access remote MySQL database

/**
 * 
 */
package ORM;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author Gwilym
 * @version 0.0
 */
public class DatabaseConnection {

 private  String userName="";
 private  String password="";
 private  String host="";
 Connection conn;

/**
  * @param userName
  * @param password
  * @param host
  */
 public DatabaseConnection(String userName, String password, String host) {
  this.userName = userName;
  this.password = password;
  this.host = host;
 }

 public DatabaseConnection(String userName, String password, String host,boolean autoConnect) {
  this.userName = userName;
  this.password = password;
  this.host = host;
  if (autoConnect)
  {
  try {
   Connect();
  } catch (DatabaseConnectionException e) {
   e.printStackTrace();
  } 
  }
 }

/**
  * @return the connection
  */
 public Connection getConn() {
  return conn;
 }

 /**
  * @param userName the userName to set
  */
 public void setUserName(String userName) {
  this.userName = userName;
 }

 /**
  * @param password the password to set
  */
 public void setPassword(String password) {
  this.password = password;
 }

 /**
  * @param host the host to set
  */
 public void setHost(String host) {
  this.host = host;
 }

/**
 * Connect, attempts to connect to the MySQL database
 * with sun JDBC 
 * & MySQL driver
 * @param none
 * @return True iff connected;
 * @return False for all else;
 * @throws DatabaseConnectionException 
 */
public boolean Connect() throws DatabaseConnectionException
{
 // Attempt to load database driver
 try
    {
        String url = "jdbc:mysql:"+host;
        System.out.println(url);
        //Load driver
        Class.forName ("com.mysql.jdbc.Driver").newInstance ();

        conn = DriverManager.getConnection (url, userName, password);
    }
 catch (ClassNotFoundException cnfe) // driver not found
 {
  conn=null;
  System.err.println ("Unable to load database driver");
  throw new DatabaseConnectionException(cnfe);
 } 
 catch(InstantiationException ie)
 {
  conn=null;
  System.err.println ("Unable to Create database driver");
  throw new DatabaseConnectionException(ie); 
 }
 catch (IllegalAccessException iae)
 {
  conn=null;
  System.err.println ("Unable to Create database driver");
  throw new DatabaseConnectionException(iae);
 } catch (SQLException sqle) {
  conn=null;
  System.err.println ("SQL error");
  throw new DatabaseConnectionException(sqle);
 }

    if (conn!=null)
    {
        System.out.println ("Database connection established"); 
        return true;
    }
    else
    {
     System.out.println ("Database connection Failed"); 
        return false;
    }

}


/**
 * Disconnects the System from the mySQL database
 * 
 * @param none
 * @return true, if successful
 * @return false if not connection in existance 
 */
public boolean Disconnect()
{
 if (conn != null)
    {
        try
        {
            conn.close ();
            conn=null;
            System.out.println ("Database connection terminated normally");
            return true;
        }
        catch (Exception e) {
       //Ignore these errors as they all result in conn.close anyway
        }
        finally
        {
        conn=null;
        System.gc();
        // my removing the refrance to conncetion all calling the Garbage collecter we insure it is destoryed.
        }
        System.out.println ("Database connection terminated with errors");
        return true;    
    }
 else
 {
  System.out.println ("No Database connection present");
        return true;    
 }
}




}

The above code is called by 上面的代码叫做

DatabaseConnection db =new DatabaseConnection("USERNAME","PASSWORD","//tel2.dur.ac.uk:3306/dcs8s07_SEG",true);

for obvious reasons I have removed the user name and password , but they can be aassumed to be correct. 由于显而易见的原因,我删除了用户名和密码,但它们可以被认为是正确的。

Right down to the problem its self I get a com.mysql.jdbc.exceptions.jdbc4.CommunicationsException when ever this code is run with the details "The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server." 直到问题它自己我得到一个com.mysql.jdbc.exceptions.jdbc4.CommunicationsException什么时候这个代码运行的详细信息“成功发送到服务器的最后一个数据包是0毫秒前。驱动程序没有收到任何来自服务器的数据包。“

My main problem at the moment is trying to discover what is actually going wrong. 我目前的主要问题是试图发现究竟出了什么问题。

In so far as I can tell the driver is being loaded correctly as my code does not throw a ClassNotFoundException, rather a SQLException of some kind. 到目前为止,我可以告诉驱动程序正确加载,因为我的代码不会抛出ClassNotFoundException,而是抛出某种类型的SQLException。

So the problem is almost certainly the connection in some way. 所以问题几乎肯定是某种方式的联系。 I can connect and query this database though a phpMyadmin located on the same server so I can assume that 我可以通过位于同一服务器上的phpMyadmin连接和查询这个数据库,所以我可以假设

1)The server is online 2)mySQL is working 3)the Username and password are correct 4) the database is present and i have the name correct 1)服务器在线2)mySQL正在工作3)用户名和密码是正确的4)数据库存在,我的名字是正确的

From this and "The driver has not received any packets from the server." 由此和“驱动程序没有从服务器收到任何数据包。” I am wondering if the URL malformed? 我想知道网址是否格式错误?

URL= jdbc:mysql://tel2.dur.ac.uk:3306/dcs8s07_SEG URL = jdbc:mysql://tel2.dur.ac.uk:3306 / dcs8s07_SEG

or there a simple setting that is incorrect on the server whihc is not allowing me to connect? 或者有一个简单的设置在服务器上是不正确的,而不允许我连接?

I have pondered on this problem and attempted several googles to no avail, so any idea would be of great help 我一直在思考这个问题并尝试了几个谷歌无济于事,所以任何想法都会有很大的帮助

thanks in advance SO! 提前谢谢!

This is a wrapped exception. 这是一个包装异常。 What's the root cause of this exception? 这个例外的根本原因是什么? Look further in the stacktrace. 在堆栈跟踪中进一步查看。

A very common root cause is java.net.ConnectException: Connection refused . 一个非常常见的根本原因是java.net.ConnectException: Connection refused I've seen this in almost 99% of the cases. 我在近99%的案例中都看到了这一点。 If this is true in your case as well, then all the possible causes are: 如果在您的情况下也是如此,那么所有可能的原因是:

  1. IP address or hostname in JDBC URL is wrong. JDBC URL中的IP地址或主机名是错误的。
  2. Hostname in JDBC URL is not recognized by local DNS server. 本地DNS服务器无法识别JDBC URL中的主机名。
  3. Port number is missing or wrong in JDBC URL. JDBC URL中的端口号丢失或错误。
  4. DB server is down. 数据库服务器已关闭。
  5. DB server doesn't accept TCP/IP connections. 数据库服务器不接受TCP / IP连接。
  6. Something in between Java and DB is blocking connections, eg a firewall or proxy. Java和DB之间的某些东西阻止了连接,例如防火墙或代理。

To solve the one or the either, follow the following advices: 要解决其中一个问题,请遵循以下建议:

  1. Verify and test them with ping . 使用ping验证并测试它们。
  2. Refresh DNS or use IP address in JDBC URL instead. 刷新DNS或使用JDBC URL中的IP地址。
  3. Verify it based on my.cnf of MySQL DB. 根据MySQL DB的my.cnf进行验证。
  4. Start it. 启动它。
  5. Verify if mysqld is started without the --skip-networking option. 验证是否在没有--skip-networking选项的情况下启动mysqld。
  6. Disable firewall and/or configure firewall/proxy to allow/forward the port. 禁用防火墙和/或配置防火墙/代理以允许/转发端口。

The username and password are irrelevant in this problem. 用户名和密码与此问题无关。 At this point the DB can't even be reached. 此时甚至无法访问DB。 You would have gotten a "Login failed" or "Not authorized" SQLException otherwise. 否则您将获得“登录失败”或“未授权” SQLException

In addition to the last post you should also do a low-level telnet test, this is the best way to verify connectivity. 除了上一篇文章,您还应该进行低级别的telnet测试,这是验证连接性的最佳方法。 This test will tell you if there is a firewall or other software blocking access to that port. 此测试将告诉您是否存在阻止访问该端口的防火墙或其他软件。

telnet tel2.dur.ac.uk 3306 telnet tel2.dur.ac.uk 3306

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: “com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败”到远程数据库 - “com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure” to remote database com.mysql.jdbc.exceptions.jdbc4.CommunicationsException MySQL + Apache Tomcat 7 - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException MySQL + Apache Tomcat 7 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败? - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure? 引起原因:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException - Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 - com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure AWS RDS连接数据库错误:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 - AWS RDS connection database error : com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure MySQL:间歇性com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 - MySQL: Intermittant com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure eclipe:间歇性com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 - eclipe: Intermittent com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 第一次尝试设置测试数据库但得到com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 - First time trying to setup a test database but gets com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM