开发者

Inserting a resultset into jtable directly

开发者 https://www.devze.com 2023-02-25 23:19 出处:网络
If is there any way to insert a resultset into jtable directly?开发者_开发技巧Bad idea. You shouldn\'t be passing anything from the java.sql package out of your persistence tier.

If is there any way to insert a resultset into jtable directly?开发者_开发技巧


Bad idea.

You shouldn't be passing anything from the java.sql package out of your persistence tier.

You can certainly iterate over a ResultSet and load the contents into your DefaultTableModel. But I wouldn't recommend it.

Something like this:

public DefaultTableModel map(ResultSet resultSet) throws SQLException
{
    DefaultTableModel defaultTableModel = new DefaultTableModel();

    ResultSetMetaData meta = resultSet.getMetaData();
    int numberOfColumns = meta.getColumnCount();
    while (resultSet.next())
    {
        Object [] rowData = new Object[numberOfColumns];
        for (int i = 0; i < rowData.length; ++i)
        {
            rowData[i] = resultSet.getObject(i+1);
        }
        defaultTableModel.addRow(rowData);
    }

    return defaultTableModel;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消