private void updateBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateBtnActionPerformed

        question = questionField.getText();
        instructions = instructionField.getText();

        // set the match patter of "[]"
        Matcher matcher = Pattern.compile("\\[([^\\]]+)").matcher(question);

        ArrayList<String> answers = new ArrayList<String>();

        int pos = -1;
        while (matcher.find(pos+1)){
            pos = matcher.start();
            // Add the matched word to the Arraylist
            answers.add(matcher.group(1));
        }

        // Find the word with bracket and replace it with blank
        String newQuestion = question.replaceAll("\\[.*?]", "_____");

        //System.out.println(answers);
        System.out.println(newQuestion);

        int key = 0;

        try {
            connectDb();

            int QType_ID = 2;

            int errors = 0;
            
            // Update the question table
            sql1 = "UPDATE fib SET QuestionText='"+question+"',QuestionBlank='"+newQuestion+"', Instructions='"+instructions+"' WHERE QuestionID='"+questionID+"' ";
            int rows = st.executeUpdate(sql1);
            if(rows == 0) {
                errors++;
            }
            
            //System.out.println(sql1);

            // Delete all the answers
            String deleteAnswer = "DELETE FROM fibanswer WHERE QuestionID='"+questionID+"'";
            int rows2 = st.executeUpdate(deleteAnswer);
            if(rows2 == 0) {
                errors++;
            }
            
            if (errors == 0) {
                JOptionPane.showMessageDialog(null, "Question was successfully updated!");
            } else {
                 JOptionPane.showMessageDialog(null, "ERROR in updating question!!", "error", JOptionPane.ERROR_MESSAGE);
            }
            
            //System.out.println(deleteAnswer);
 
        }
        catch (Exception exc) {
            exc.printStackTrace();
        }
 
        // Loop through ArrayList and Insert NEW Answers into DB
        for (String answer : answers) {

            try {
                connectDb();

                
                // Insert answer to DB
                sql2 = "INSERT INTO fibanswer (AnswerContentText,QuestionID) VALUES('"+answer+"', '"+questionID+"')";
                st.executeUpdate(sql2);

                System.out.println(sql2);

            }
            catch (Exception exc) {
                exc.printStackTrace();
            }

        }


    }//GEN-LAST:event_updateBtnActionPerformed