简体   繁体   English

JTable Swing检索数据

[英]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). (因此,该表接受数据的Object [] []参数)。 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 . 两年前,在我上技术学校的那段时间里,我写了一个图书馆帮助解决了练习中提出的一些问题,其中包括一个DatabaseTableModel

The class extends from AbstractTableModel , which means you can set it as the your JTable 's data source. 该类从AbstractTableModel扩展而来, 这意味着您可以将其设置JTable的数据源。

Here's the algorithm that constructs a model from a ResultSet : 这是从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. 一个问题是它将ResultSet全部内容加载到内存中。 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. 不过,我认为这是一个很好的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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