2014-11-14 27 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