当前位置: 首页 > 知识库问答 >
问题:

ECLIPE:间断com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

公冶弘壮
2023-03-14

下面是我用于所有MySQL连接的代码:

public class MySQLAccess {
    private Connection connect = null;
    private Statement statement = null;
    private ResultSet resultSet = null;
    public String query = null;
    public int lastId = 0;

    private Connection getConnection() throws Exception {
        // reads a config properties file
        AWBConfig Config = new AWBConfig();
        String host = Config.Data.getProperty("mysql.host");
        if (!Config.Data.getProperty("mysql.port").isEmpty()) host += ":" + Config.Data.getProperty("mysql.port");
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://"+host+"/" + Config.Data.getProperty("mysql.name") + "?user=" + 
            Config.Data.getProperty("mysql.user") + "&password=" + Config.Data.getProperty("mysql.pass"));
    }

    public ResultSet readDataBase() throws Exception {
        if (connect == null || connect.isClosed()) connect = getConnection();
        statement = connect.createStatement();
        resultSet = statement.executeQuery(query);
        return resultSet;
    }

    public ResultSet readDataBase(String _query) throws Exception {
        this.query = _query;
        resultSet = this.readDataBase();
        return resultSet;
    }

    public void writeData() throws Exception {
        if (connect == null || connect.isClosed()) connect = getConnection();
        statement = connect.createStatement();
        if (query.substring(0, 6).equals("INSERT")) {
            statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
            ResultSet rs = statement.getGeneratedKeys();
            if (rs.next()){
                this.lastId=rs.getInt(1);
            }
            rs.close();
        } else {
            statement.executeUpdate(query);
            this.lastId = -1;
        }
    }

    public void close() {
        try {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connect != null) connect.close();
        } catch (Exception e) {
        }
    }
}

然后与数据库交互,我使用以下格式:

    protected void getCampaignById() throws Exception {
        _dba = new MySQLAccess();
        _dba.query = "SELECT * FROM table WHERE id = '" + String.valueOf(id) + "'";
        ResultSet rs = _dba.readDataBase();
        if (rs.next()) {
            ... do stuff with rs
        }
        _dba.close();
    }

共有1个答案

宇文俊明
2023-03-14

嗯,我不确定问题到底是什么,但我在核心应用程序的main()方法中将_dba改为静态变量,然后在每个类构造函数中添加了以下一行:

_dba = MyMainClass._dba;

然后从各处删除这些行(MyMainClass.Main()除外):

_dba = new MySQLAccess();
...
_dba.close();

使数据库连接成为一个全局持久连接,这不仅解决了我的错误,而且给了我一个巨大的性能提升。

 类似资料:
  • 问题内容: 我正在努力使数据库与Java程序进行通讯。 有人可以使用JDBC给我一个快速而肮脏的示例程序吗? 我收到一个非常严重的错误: 测试文件的内容: 问题答案: 所以,你有一个 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 java.net.ConnectException:连接被拒绝 我引用了这个答案,其中还包

  • 我正在使用hibernate.cfg运行Spring MVC应用程序,如下所示: 当我在本地环境中运行应用程序时,它运行起来没有任何问题。但在服务器上,它给出的异常为:

  • 问题内容: 我正在使用Mac OS X Lion版本10.8。我通过XAMPP安装了MySQL。并且我已经将mysql和jdbc驱动程序(5.1.22)添加到了我的类路径中。以下简单代码无法正常工作: 运行代码时,出现以下错误: 有谁知道如何解决这个问题? 问题答案: 我终于弄明白了。感谢Max和Yogendra发布。问题出在我的XAMPP配置上。要解决此问题,我做了如下操作: 然后我运行我的代码

  • 问题内容: 我使用Java从Mysql查询一些记录。但是在某些持续时间的查询中,我遇到了使查询失败的问题,而在其他查询中,它查询成功了。错误消息是下一个: 我尝试了一些方法,例如: 设置 在 添加 到我的连接网址 但什么也没发生。 我的环境是: Mysql:5.5.3-m3-log源代码分发 的Java:1.6.0_16 JDK:HotSpot(TM)64位服务器VM(内部版本14.2-b01,混

  • 问题内容: 我已经遇到了几天的问题,这是详细信息堆栈信息: 我想通过mybatis和spring更新mysql中的一条记录,然后一个接一个地更新它,下面是mybatis config:更新建议集count =#{count},version =#{version},freq =#{freq}其中id =#{id} 以及服务代码: 有人可以帮我吗?非常感谢。 问题答案: 你的mySQL连接在连接池识

  • 问题内容: 这个问题已经在这里有了答案 : “软件导致连接中止:套接字写入错误”的官方原因 (14个答案) 3年前关闭。 所有。我已经遇到了几天的问题,这是详细信息堆栈信息: 我想通过mybatis和spring更新mysql中的一条记录,然后一个接一个地更新它,下面是mybatis config:更新建议集count =#{count},version =#{version},freq =#{f