简体   繁体   中英

JTable Swing retrieve data

I'm trying to populate a table with data from a database however i am having some issues with it. Could someone provide me with an example? (so the table takes in an Object[][] parameter for the data). I have the following basic code to display a table ;

class table extends JFrame
{
    JTable table; 

    public table()
    {
        setLayout(new FlowLayout());
        String[] columnNames = {"test","test","test"};
        Object[][] data= {{"test","test","test"},{"test","test","test"}};

        table = new JTable(data,columnNames);
        table.setPreferredScrollableViewportSize(new Dimension(500,100));
        table.setFillsViewportHeight(true);

       JScrollPane scrollPane = new JScrollPane(table);
       add(scrollPane); 
    }
}

Two years ago, during my time in technical school, I wrote a little library help solve some of the problems proposed by the exercises, which included aa DatabaseTableModel .

The class extends from AbstractTableModel , which means you can set it as the your JTable 's data source.

Here's the algorithm that constructs a model from a ResultSet :

public final void constructModel(ResultSet rs) throws SQLException {
    ResultSetMetaData rsmd = rs.getMetaData();
    rs.last();
    rowCount = rs.getRow();
    int columnCount = rsmd.getColumnCount();
    // DatabaseColumn simply holds a name and a Class<?>.
    columns = new DatabaseColumn[columnCount];
    // This is the Object[][] array that you were talking about.
    // It holds all the data from the ResultSet.
    data = new Object[columnCount][rowCount];
    for (int i = 0; i < columnCount; ++i) {
        // Figure out the column name and type.
        int j = i + 1;
        String colName = rsmd.getColumnLabel(j);
        Class<?> colClass = String.class;
        try {
            colClass = Class.forName(rsmd.getColumnClassName(j));
        } catch (ClassNotFoundException ex) {
            colClass = String.class;
        }
        columns[i] = new DatabaseColumn(colName, colClass);
        // Get the data in the current column as an Object.
        rs.beforeFirst();
        for (int k = 0; rs.next(); ++k) {
            data[i][k] = rs.getObject(j);
        }
    }
    // Notify listeners about the changes so they can update themselves.
    fireTableStructureChanged();
}

The class worked when I used it in school, but it isn't exactly production code. When I look at it today, I start to see problems.

One problem is that it is loading the entire contents of the ResultSet into memory. Could get ugly pretty quickly.

Also, the algorithm isn't exactly optimal. It loops around with the database cursor as if it was nothing; I suppose that it would be less costly for the database if it had retrieved all the objects in the current row first and assigned them to their appropriate columns before moving on to the next row.

Nevertheless, I think it is a good enough starting point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM