MySQL连接的进度条 [英] Progress bar for MySQL connection

查看:159
本文介绍了MySQL连接的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JDBC将我的程序链接到MySQL。



我注意到建立到MySQL的连接可能需要大约6-10秒。
我想使用JProgressBar显示连接的进度,以便用户不必认为我的程序崩溃了。



我试图插入一个简单的打印语句前后的代码。创建连接后的打印语句只在6-10秒后打印。



我想在连接之间取得进度。


$ b $

解决方案

p>我想你可以显示一个不确定的 JProgressBar (参见如何使用进度栏),因为你不能完全知道在给定时间的进度。还可以使用 SwingWorker 在后台建立连接,避免阻止 EDT 。小代码片段:

  public static void main(String [] args){
JFrame frame = new JFrame测试);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.setLocationRelativeTo(null);

final JProgressBar jProgressBar = new JProgressBar();
final JLabel status = new JLabel(Connecting ...);
frame.add(status);
frame.add(jProgressBar,jProgressBar);

frame.pack();
frame.setVisible(true);

SwingWorker sw = new SwingWorker(){
@Override
protected Object doInBackground()throws Exception {
jProgressBar.setIndeterminate(true);
Thread.sleep(6000); //这里你应该建立连接
return null;
}

@Override
public void done(){
jProgressBar.setIndeterminate(false);
status.setText(Successful);
jProgressBar.setValue(100); // 100%
}
};
sw.execute();
}

你会看到这样的:



>


I am using JDBC to link my program to MySQL.

I noticed that establishing a connection to MySQL can take about 6-10 seconds. I want to use a JProgressBar to show the progress of the connection so that the user needen't think that my program crashed.

I tried inserting a simple print statement before and after the code. The print statement after the create connection printed only after 6-10 seconds.

I want to get progress between the connection.

Please I am not asking for you to do the full coding etc. Kindly tell me how I could acheive it?

解决方案

I think you can show an indeterminate JProgressBar (see How to Use Progress Bar) since you can't exactly know the progress at a given time. Also use SwingWorker to establish your connection at background avoiding to block EDT. Little code snippet:

 public static void main(String[] args) {
    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    frame.setLayout(new FlowLayout());
    frame.setLocationRelativeTo(null);

    final JProgressBar jProgressBar = new JProgressBar();
    final JLabel status = new JLabel("Connecting...");
    frame.add(status);
    frame.add("jProgressBar", jProgressBar);

    frame.pack();
    frame.setVisible(true);

    SwingWorker sw = new SwingWorker() {
        @Override
        protected Object doInBackground() throws Exception {
            jProgressBar.setIndeterminate(true);
            Thread.sleep(6000); // Here you should establish connection
            return null;
        }

        @Override
        public void done(){
            jProgressBar.setIndeterminate(false);
            status.setText("Successful");
            jProgressBar.setValue(100); // 100%
        }
    };
     sw.execute();        
}

And you'll see something like this:

这篇关于MySQL连接的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆