Java 请求并发的无响应jbutton

Java 请求并发的无响应jbutton,java,multithreading,swing,concurrency,swingworker,Java,Multithreading,Swing,Concurrency,Swingworker,有人能帮我实现无响应jbutton的线程吗?我想使用pepraredStatement插入到数据库中。但是,每当我添加数据库部分(connection和pepraredStatement)时,“UPLOAD”按钮就没有响应。但是当我删除任何与数据库相关的内容时,我所有的按钮都在工作。你可以在这里找到代码 我将非常感谢任何帮助或建议。我已经对线程进行了解释,但仍然无法将其包含到代码中 public void actionPerformed(ActionEvent ev) {

有人能帮我实现无响应jbutton的线程吗?我想使用pepraredStatement插入到数据库中。但是,每当我添加数据库部分(connection和pepraredStatement)时,“UPLOAD”按钮就没有响应。但是当我删除任何与数据库相关的内容时,我所有的按钮都在工作。你可以在这里找到代码

我将非常感谢任何帮助或建议。我已经对线程进行了解释,但仍然无法将其包含到代码中

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)
                    {

                    }
public void actionPerformed(ActionEvent ev)
{
String file=fileField.getText();
SetGetQuestionFileName模式=新的SetGetQuestionFileName(文件);
ConnectToDatabase数据库=新的ConnectToDatabase();
尝试
{
/////////检查文本文件是否为空
if(ev.getActionCommand().equals(“上载”))
{
if(fileField.getText().isEmpty())
{
JOptionPane.showMessageDialog(null,“文件字段不能为空!!!请重试。”,“警报”,JOptionPane.ERROR\u消息);
}
其他的
{
File fi=新文件(fileField.getText());
////////////////执行上传
尝试
{
字符串sql=“插入testsystem.inventory(类别问题、问题、正确答案)”+“值(?,?)”;
PreparedStatement st=null;
Connection dbconnection=database.getConnection();
st=dbconnection.prepareStatement(sql);
如果(fi.getAbsoluteFile().exists())
{
列表行=Files.readAllLines(path.get(fileField.getText()),Charset.defaultCharset());
对于(int i=0;i
当你有一个动作时,强烈建议你封装你的代码来完成这项工作。在你的情况下,是db(称之为DBThread)。对你来说,这样按钮就不会失去响应了,那就是把它分成3个部分(起初听起来太多了,但很有意义)。所以你就有了

  • 操作(调用DBThreadProgress的)
  • DBThreadProgress(它是一个线程,在末尾有一个连接并将调用DBThread)
  • DBThread(这是作业)
  • 因此,前面提到的DBThread是您的业务逻辑。该操作调用一个进度线程,在该线程中,您可以在面板上放置一个gif图像,显示最终用户在后台发生的事情。该进度线程还允许您等待DBThread完成,以便您可以向最终用户建议结果。我很高兴看到y在开始时将按钮设置为setEnable(false)和setEnable(true),以便用户知道他不能单击两次

    希望这有帮助:-)

    e.printStackTrace()
    放入
    catch
    块中。您的问题是:您在GUI线程中提供了耗时的操作(调用EDT-事件调度程序线程)。只需将数据库代码移动到另一个线程中,例如使用.Read,您就会更好地理解您的问题。这就是我在讲师$MyAction.actionPerformed(讲师.java:183)处得到的错误java.lang.NullPointerException,第183行是这一行st=dbconnection.prepareStatement(sql);Sergiy Medvynsky如果你不介意的话,你能解释一下我如何从发布的代码中做到这一点吗