使用jbutton问题保存到mysql数据库

时间:2014-11-14 21:33:18

标签: java swing jdbc swingworker

美好的一天。任何人真的可以帮助我解决我的数据库问题吗?我想使用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();             尝试             {

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

Swing是一个单线程框架。任何长时间运行或阻塞的操作,当在Swing的GUI线程(事件调度线程)的上下文中执行时,将阻止它更新屏幕。

请查看Concurrency in SwingWorker Threads and SwingWorker了解详情

相关问题