Search code examples
javaswingjdbcswingworker

Saving to mysql database with jbutton issue


Good day . Can anyone really help me with what i'm facing as issue with my database ? i want to insert to the database by using the pepraredStatement . however whenever i'm adding the database part (connection and pepraredStatement ) the 'UPLOAD' button goes unresponsive . but when i remove anything related to the database , all my buttons are working . you can find here the code http://pastebin.com/euKdWhr2 .

I will really appreciate any help or suggestion . probably i'm missing something on the database part .

And please it is not a duplicated question , because i did not find the right answer 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)
                {

                }

Solution

  • Swing is a single threaded framework. Any operation which is long running or blocking, when executed within the context of the Swing's GUI thread (the Event Dispatching Thread) will prevent it from updating the screen.

    Take a look at Concurrency in Swing and Worker Threads and SwingWorker for more details