gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 05:00:08 26 4
gpt4 key购买 nike

/**
*
*/
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;
}
}




}

上面的代码被调用

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

出于显而易见的原因,我删除了用户名和密码,但可以假定它们是正确的。

问题本身我得到一个 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException 每当这个代码运行时有详细信息“最后一个数据包成功发送到服务器是 0 毫秒前。驱动程序有没有收到来自服务器的任何数据包。”

我目前的主要问题是试图发现实际出了什么问题。

据我所知,驱动程序正在正确加载,因为我的代码没有抛出 ClassNotFoundException,而是某种 SQLException。

所以问题几乎可以肯定是某种方式的连接。我可以通过位于同一台服务器上的 phpMyadmin 连接并查询此数据库,因此我可以假设

1)服务器在线2)mySQL正在工作3)用户名和密码正确4) 数据库存在并且我的名字是正确的

从这个和“驱动程序没有从服务器收到任何数据包。”我想知道 URL 是否格式错误?

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

或者服务器上有一个不正确的简单设置不允许我连接?

我已经思考过这个问题并尝试了几次谷歌都无济于事,所以任何想法都会有很大帮助

提前致谢!

最佳答案

这是一个包装异常。此异常的根本原因是什么?在堆栈跟踪中进一步查看。

一个非常常见的根本原因是 java.net.ConnectException: Connection refused。我在几乎 99% 的案例中都看到过这种情况。如果您的情况也是如此,那么所有可能的原因都是:

  1. JDBC URL 中的 IP 地址或主机名错误。
  2. 本地 DNS 服务器无法识别 JDBC URL 中的主机名。
  3. JDBC URL 中的端口号缺失或错误。
  4. 数据库服务器已关闭。
  5. 数据库服务器不接受 TCP/IP 连接。
  6. Java 和 DB 之间的某些东西正在阻塞连接,例如防火墙或代理。

要解决其中一个问题,请遵循以下建议:

  1. 使用 ping 验证和测试它们。
  2. 刷新 DNS 或改用 JDBC URL 中的 IP 地址。
  3. 根据MySQL数据库的my.cnf验证。
  4. 开始吧。
  5. 验证 mysqld 是否在没有 --skip-networking 选项的情况下启动。
  6. 禁用防火墙和/或配置防火墙/代理以允许/转发端口。

用户名和密码与此问题无关。此时甚至无法访问数据库。否则,您会收到“登录失败”或“未授权”SQLException

关于java - 使用JDBC访问远程MySQL数据库时出现com.mysql.jdbc.exceptions.jdbc4.CommunicationsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1986902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com