无响应的jbutton请求并发

时间:2014-11-14 23:04:59

标签: java multithreading swing concurrency swingworker

任何人都可以帮我实现我的无响应jbutton的线程吗?我想使用pepraredStatement插入数据库。但是,每当我添加数据库部分(连接和pepraredStatement)时,上传' UPLOAD'按钮没有响应。但当我删除任何与数据库相关的内容时,我的所有按钮都正常工作。你可以在这里找到代码http://pastebin.com/euKdWhr2

我将非常感谢任何帮助或建议。我对该主题有解释,但我仍然无法将其包含在我的代码中。

public void actionPerformed(ActionEvent ev)
        {
            String file = fileField.getText();
            SetGetQuestionFileName pattern = new SetGetQuestionFileName(file);
                ConnectToDatabase database = new ConnectToDatabase();
            try
            {

            ///////// check whether textfile is empty or not 

            if( ev.getActionCommand().equals("UPLOAD"))
            {
                if(fileField.getText().isEmpty())
                {
                    JOptionPane.showMessageDialog(null,"File field can not be empty!!! Please try again.","ALERT", JOptionPane.ERROR_MESSAGE);
                }
                else
                    {
                        File fi = new File(fileField.getText());
                        ////////////////  perform upload 

                        try 
                            {

                    String sql = "INSERT INTO testsystem.questionnaire (category_questions, questions, correct_answer)" + "VALUES (?, ?, ?)";

                    PreparedStatement st =  null;

                    Connection dbconnection = database.getConnection();

                    st = dbconnection.prepareStatement(sql);

                                    if(fi.getAbsoluteFile().exists())
                                    {
                                        List<String> lines = Files.readAllLines(Paths.get(fileField.getText()), Charset.defaultCharset());


                                        for (int i = 0; i < lines.size(); i+=10) 
                                            {
                                                    String category = lines.get(i);
                                                    System.out.println(category);
                                                    String question = lines.get(i+1);
                                                   System.out.println(question);

                                                    String answers =
                                                                    lines.get(i+2)+System.lineSeparator()
                                                                    +lines.get(i+3)+System.lineSeparator()
                                                                    +lines.get(i+4)+System.lineSeparator()
                                                                    +lines.get(i+5);
                                                    System.out.println(answers);

                                                    String correct = lines.get(i+7);
                                                    System.out.println("correct answer is: "+correct);
                                                    System.out.println("----------------");


                                    st.setString(1, category);
                                    st.setString(2, answers);
                                    st.setString(3, correct);
                                    st.executeUpdate(); 

                                        }

                                        JOptionPane.showMessageDialog(null,"File has been successfully uploaded in the database.","NOTIFCATION",JOptionPane.INFORMATION_MESSAGE);
                                    }
                else

                        JOptionPane.showMessageDialog(null,"File could not be found. Please try again","ALERT",JOptionPane.ERROR_MESSAGE);
                }

                        catch(SQLException ex)
                    {

                    }   

                    catch(Exception ex)
                    {

                    }

1 个答案:

答案 0 :(得分:2)

当您采取行动时,强烈建议您封装实际完成工作的代码。在你的情况下,它是db(称之为DBThread)。什么对你有用,所以按钮不会无响应是将它分成3个部分(起初听起来太多但是有意义)。所以你有

  1. Action(调用DBThreadProgress)
  2. DBThreadProgress(它是一个最后将有一个连接并将调用DBThread的线程)
  3. DBThread(这是工作)
  4. 所以前面提到的DBThread就是你的业务逻辑。 Action会调用一个进度线程,您可以在面板上放置一个gif图像,该图像显示最终用户的背景状态。此progressThread允许您等到DBthread完成,以便您可以向最终用户建议结果。我通常将按钮设置为开头的setEnable(false)和setEnable(true),以便用户知道他不能点击两次。

    希望这会有所帮助:-) enter image description here