【问题标题】:JTextArea refreshment while SQL query running using java使用java运行SQL查询时JTextArea刷新
【发布时间】:2018-08-11 23:27:48
【问题描述】:

我有一个 TextArea 对象,并且正在运行 SQL 查询,在 SQL 查询完成之前我的 GUI 无法使用,我想同时刷新 GUI。

【问题讨论】:

标签: java multithreading swing user-interface event-dispatch-thread


【解决方案1】:

您的 SQL 查询正在 Event Dispatch Thread (EDT) 上运行,这会阻止 GUI 自行更新,直到长时间运行的任务完成。

您需要在单独的Thread 上运行查询。

一个简单的方法是使用Swing Worker

阅读 Concurrency 上的 Swing 教程部分,了解有关 EDT 的更多信息以及使用 SwingWorker 的示例。

【讨论】:

    【解决方案2】:

    私有字符串 printCommandCalled(字符串参数)抛出 SQLException { 字符串最终结果 = ""; url = url.substring(0, 15 + port.length() + adress.length()).concat(currentDatabaseName + "?useSSL=false"); 尝试 { connection = (Connection) DriverManager.getConnection(url, userName, password); statement = (Statement) connection.createStatement(); resultSet = (ResultSet) statement.executeQuery("SELECT * FROM " + argument + ";"); } 捕捉(SQLException e){ } int i = resultSet.getMetaData().getColumnCount();

        //this is the try block that takes too long resulting the unresponsiveness of the gui
        try {
    
            while (resultSet.next()) {
                for (int j = 1; j <= i; j++)
                    finalResult += resultSet.getObject(j) + "      ";
                finalResult += '\n';
            }
    
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        for (int k = 1; k <= i; k++)
            finalResult += resultSet.getMetaData().getColumnName(k) + " ("
                    + resultSet.getMetaData().getColumnTypeName(k) + ")      ";
        finalResult += '\n';
    
        //this output is a string that will be shown in the JTextArea object
        return finalResult;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-25
      • 2022-11-16
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-15
      相关资源
      最近更新 更多