anton86993 anton86993 - 1 year ago 57
Java Question

Java with SwingWorker - GUI freezes after button pressed (ProgressBar not working too)

The main idea of my program is to parse XML data from file and insert it into MySQL database using JDBC driver. File conatins 2000 elements and everything works perfectly - file is parsed and data loaded to database. The problem is that when I press button to start it all GUI freeze and the JProgressBarr does not work as it intended. I have no idea why. I used SwingWorker to do thread work. Here is my code:

package javamysqlinsert;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.HashMap;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class JavaMySqlInsert extends JFrame implements ActionListener {

private final String DEFAULT_DRIVER = "com.mysql.jdbc.Driver";
private final String DB_URL = "jdbc:mysql://"
+ "anton869_cars";
private final String DB_USER = "user";
private final String DB_PASSWORD = "user";

private JTextField xmlPathField;
private JButton chooseFileButton, insertDataButton;
private JProgressBar insertProgressBar;
private String filePath;

class LoadXmlToDatabase extends SwingWorker<Void, Void> {

public Void doInBackground() {
return null;

public void done() {
"Finished loading data to MySQL database",
"Loading success", JOptionPane.INFORMATION_MESSAGE);

private void loadAndParseXml() {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(JavaMySqlInsert.this
System.out.println("Successfully loaded XML file");
} catch (IOException | ParserConfigurationException
| SAXException e) {

private void fillItemList(final Document doc) {
try {
System.out.println("Connecting to MySQL database...");
Connection conn = DriverManager.getConnection(DB_URL, DB_USER,
System.out.println("Connected to MySQL database");

System.out.println("Starting parsing XML document and loading "
+ "it to MySQL database...");
NodeList nodesList = doc.getElementsByTagName("T");
final int nodeListLength = nodesList.getLength();
for (int i = 0; i < nodeListLength; i++) {
Node node = nodesList.item(i);
Element element = (Element) node;
HashMap<String, String> carMap = new HashMap<>();
if (node.getNodeType() == Node.ELEMENT_NODE) {
// <P_NAME>
carMap.put("p_name", element
// <P_MFGR>
carMap.put("p_mfgr", element
// <P_BRAND>
carMap.put("p_brand", element
// <P_TYPE>
carMap.put("p_type", element
// <P_SIZE>
carMap.put("p_size", element
carMap.put("p_container", element
carMap.put("p_retailprice", element
carMap.put("p_comment", element
insertCarInfoRow(conn, carMap);
System.out.println("Data have been successfully parsed and "
+ "loaded to MySQL database. Inserted: "
+ nodeListLength + " rows");
} catch (SQLException | ClassNotFoundException e) {

private void insertCarInfoRow(final Connection conn,
final HashMap<String, String> carMap) {
final String insertQuery = "INSERT INTO part (p_name, p_mfgr, "
+ "p_brand, p_type, p_size, p_container, p_retailprice, "
+ "p_comment) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

try (PreparedStatement ps = conn.prepareStatement(insertQuery)) {
ps.setString(1, carMap.get("p_name"));
ps.setString(2, carMap.get("p_mfgr"));
ps.setString(3, carMap.get("p_brand"));
ps.setString(4, carMap.get("p_type"));
ps.setInt(5, Integer.parseInt(carMap.get("p_size")));
ps.setString(6, carMap.get("p_container"));
ps.setFloat(7, Float.parseFloat(carMap.get("p_retailprice")));
ps.setString(8, carMap.get("p_comment"));
} catch (SQLException e) {


public JavaMySqlInsert() {

public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(this.insertDataButton)) {
if (testInternetConnection("")) {
new LoadXmlToDatabase().run();
} else JOptionPane.showMessageDialog(this,
"You have no Internet connection", "Network error",

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {

public void run() {
JavaMySqlInsert jmsqli = new JavaMySqlInsert();



Answer Source

Change this new LoadXmlToDatabase().run(); to this new LoadXmlToDatabase().execute();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download