2014-11-14 69 views
0

美好的一天。任何人都可以真正帮我解决我的数据库问题吗?我想通过使用pepraredStatement插入到数据库。然而,无论何时添加数据库部分(connection和pepraredStatement),'UPLOAD'按钮都不响应。但是当我删除任何与数据库相关的东西时,我所有的按钮都在运行。你可以在这里找到代码http://pastebin.com/euKdWhr2用jbutton问题保存到mysql数据库

我会很感激任何帮助或建议。可能我在数据库部分丢失了一些东西。

并请它不是一个重复的问题,因为我没有找到正确的答案 公共无效的actionPerformed(ActionEvent的EV) { 字符串文件= 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) 
       { 

       } 

回答

2

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

看看Concurrency in SwingWorker Threads and SwingWorker更多细节

+0

我真的很努力学习Java和部分我并没有真正得到了线程,所以如果你不介意的话,你可以引导一个我从我的部分代码 – mkwilfreid 2014-11-14 21:38:11

+0

我现在不在我的PC上,你可能会考虑进行一次快速入侵,看看你能得到什么,然后我会尝试发布一个小例子 – MadProgrammer 2014-11-14 23:05:18