Home > Java > javaTutorial > How to Populate a JTable from a ResultSet and Avoid IllegalStateException?

How to Populate a JTable from a ResultSet and Avoid IllegalStateException?

Linda Hamilton
Release: 2024-12-01 05:40:17
Original
136 people have browsed it

How to Populate a JTable from a ResultSet and Avoid IllegalStateException?

Populating JTable from a ResultSet

Problem Description

Within a GUI application, a user is encountering an IllegalStateException when updating a DefaultTableModel from a ResultSet. The goal is to display the contents of the ResultSet within a JTable.

Solution 1

The following code provides a straightforward method to create a DefaultTableModel from a ResultSet:

public static DefaultTableModel buildTableModel(ResultSet rs)
        throws SQLException {

    ResultSetMetaData metaData = rs.getMetaData();

    // Column names
    Vector<String> columnNames = new Vector<>();
    int columnCount = metaData.getColumnCount();
    for (int column = 1; column <= columnCount; column++) {
        columnNames.add(metaData.getColumnName(column));
    }

    // Table data
    Vector<Vector<Object>> data = new Vector<>();
    while (rs.next()) {
        Vector<Object> vector = new Vector<>();
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            vector.add(rs.getObject(columnIndex));
        }
        data.add(vector);
    }

    return new DefaultTableModel(data, columnNames);

}
Copy after login

Utilizing this method, the table can be constructed as follows:

JTable table = new JTable(buildTableModel(rs));
Copy after login

Solution 2 (Using SwingWorker)

This solution employs a SwingWorker to perform the data loading in a separate thread, enhancing the UI responsiveness. It also utilizes the try-with-resources statement for improved resource management:

public class GUI extends JFrame {

    // ...

    private void loadData() {
        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();

            // ...

            while (rs.next()) {
                // ...
            }

            tableModel.setDataVector(data, columnNames);
        } catch (Exception e) {
            // ...
        }
        button.setEnabled(true);
    }

}
Copy after login

The above is the detailed content of How to Populate a JTable from a ResultSet and Avoid IllegalStateException?. 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