Home > Java > javaTutorial > How to Avoid 'IllegalStateException' When Populating a JTable from a ResultSet?

How to Avoid 'IllegalStateException' When Populating a JTable from a ResultSet?

Linda Hamilton
Release: 2024-12-21 09:46:10
Original
453 people have browsed it

How to Avoid

How to Populate a JTable from a ResultSet

Problem:

You're experiencing issues populating a JTable from a ResultSet and receiving an "IllegalStateException" error.

Answer:

Using a SwingWorker and try-with-resource Statements:

To simplify the code and utilize advanced features, consider using a SwingWorker and the try-with-resource statement. The SwingWorker allows background tasks to be performed without interfering with the GUI thread. The try-with-resources statement ensures that resources are properly closed.

Here's an example:

import java.sql.*;
import javax.swing.*;
import javax.swing.table.*;

public class GUI extends JFrame {

    // ... (remaining GUI code)

    private void loadData() {
        LOG.info("START loadData method");

        button.setEnabled(false);

        try (Connection conn = DriverManager.getConnection(url, usr, pwd);
             Statement stmt = conn.createStatement()) {

            ResultSet rs = stmt.executeQuery("select * from customer");
            ResultSetMetaData metaData = rs.getMetaData();

            // Names of columns
            Vector<String> columnNames = new Vector<>();
            int columnCount = metaData.getColumnCount();
            for (int i = 1; i <= columnCount; i++) {
                columnNames.add(metaData.getColumnName(i));
            }

            // Data of the table
            Vector<Vector<Object>> data = new Vector<>();
            while (rs.next()) {
                Vector<Object> vector = new Vector<>();
                for (int i = 1; i <= columnCount; i++) {
                    vector.add(rs.getObject(i));
                }
                data.add(vector);
            }

            tableModel.setDataVector(data, columnNames);
        } catch (Exception e) {
            LOG.log(Level.SEVERE, "Exception in Load Data", e);
        }
        button.setEnabled(true);

        LOG.info("END loadData method");
    }

}
Copy after login

This code utilizes a SwingWorker to perform the data loading in the background. It also leverages the try-with-resources statement to ensure that the connection and statement are properly closed.

Remember to replace url, usr, and pwd with the appropriate values for your database connection.

The above is the detailed content of How to Avoid 'IllegalStateException' When Populating a JTable from a ResultSet?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template