Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将SQL连接传递给ActionListener(线程)_Java_Sql_Swing_Actionlistener - Fatal编程技术网

Java 将SQL连接传递给ActionListener(线程)

Java 将SQL连接传递给ActionListener(线程),java,sql,swing,actionlistener,Java,Sql,Swing,Actionlistener,如何将SQL连接传递给动作侦听器。我想要一个无限循环,它可以休眠100毫秒。假设循环的每次迭代都要查询一个数据库。摆动计时器是最好的方法吗?如果是这样,我如何将连接传递给动作侦听器。如果没有,请有人就如何做到这一点提出建议。非常感谢 代码: 不要使用javax.swing.Timer类定期执行非swing任务。相反,使用 如果后台任务必须持续更新Swing组件,请使用SwingWorker到process()定期更新组件的模型。在这种情况下,JTextArea使用从H2数据库获得的数据进行更新。

如何将SQL连接传递给动作侦听器。我想要一个无限循环,它可以休眠100毫秒。假设循环的每次迭代都要查询一个数据库。摆动计时器是最好的方法吗?如果是这样,我如何将连接传递给动作侦听器。如果没有,请有人就如何做到这一点提出建议。非常感谢

代码:


不要使用
javax.swing.Timer
类定期执行非swing任务。相反,使用


如果后台任务必须持续更新Swing组件,请使用
SwingWorker
process()
定期更新组件的模型。在这种情况下,
JTextArea
使用从H2数据库获得的数据进行更新。

为什么要使用
javax.swing.Timer
?我看不出你在哪里使用任何Swing组件。也许您应该使用
Executors
/
ScheduledExecutorService
类?所以我应该将ScheduledExecutorService exec=Executors.newSingleThreadScheduledExecutor();在我的主类中加入一部分,然后将其余部分放在另一个方法中并调用该方法?好的,但是如何将连接传递到ScheduledExecutorService?
public static void main(String[] args) throws InterruptedException {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    AdminManager frame = new AdminManager();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });

        BoneCP connectionPool = null;
        Connection connection = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            e.printStackTrace();
            return;
        }

        try {
            // setup the connection pool
            BoneCPConfig config = new BoneCPConfig();
            config.setJdbcUrl("jdbc:mysql://192.162.0.0");
            config.setUsername("root"); 
            config.setPassword("");
            connectionPool = new BoneCP(config); // setup the connection pool

            connection = connectionPool.getConnection(); // fetch a connection

            if (connection != null){
                System.out.println("Connection successful!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }


        // Define listner
        ActionListener taskPerformer = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                //...Perform a task...

                String sql = "SELECT * table;";
                Statement st = connection.createStatement();
                ResultSet rs = st.executeQuery(sql);
                while(rs.next()) {
                    String symbol = rs.getString("name");
                    System.out.println(symbol);
                }

            }
            };
        Timer timer = new Timer( 100 , taskPerformer);
        timer.setRepeats(true);
        timer.start();

        Thread.sleep(1000);

        //connectionPool.shutdown(); // shutdown connection pool.
}
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.schedule(new Runnable(){
    @Override
    public void run(){
        // query database
    }
}, 0, 100, TimeUnit.MILLISECONDS);