如何用数据库在JTable中填充数据? [英] How to fill data in a JTable with database?

查看:31
本文介绍了如何用数据库在JTable中填充数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个 JTable,它按原样显示来自 DataBase 表的数据.

I want to display a JTable that display the data from a DataBase table as it is.

到目前为止,我一直使用 JTable 来显示来自 Object [ ][ ] 的数据.

Up till now, I have used JTable that displays data from Object [ ][ ].

我知道显示数据的一种方法是首先将数据库表转换为 Object [ ][ ] 但是有没有其他更简单但更强大和灵活的方法.

I know one way to display the data is to first convert the database table into Object [ ][ ] but Is there any other which is easy yet more powerful and flexible.

推荐答案

我建议采用以下方法:

  • 创建一个 Row 类来表示从 ResultSet 中读取的行.这可能是一个围绕 Object[] 的简单包装器.
  • 创建一个 List 集合,并将 AbstractTableModel 子类化以得到该集合的支持.
  • 使用 SwingWorker 通过从 后台线程ResultSet 读取来填充您的 List>(即在 doInBackground() 方法中).调用 SwingWorkerpublish 方法将 Row 发布回 Event Dispatch 线程(例如每 100 行).
  • SwingWorkerprocess 方法被调用并读取最新的 Rows 块时,将它们添加到您的 List 和触发适当的 TableEvent 以导致显示更新.
  • 此外,使用ResultSetMetaData 来确定TableModel 定义中每一列的Class.这将导致它们被正确渲染(如果您只是使用 2D Object[][] 数组,则不会出现这种情况).
  • Create a Row class to represent a row read from your ResultSet. This could be a simple wrapper around an Object[].
  • Create a List<Row> collection, and subclass AbstractTableModel to be backed by this collection.
  • Use a SwingWorker to populate your List<Row> by reading from the underlying ResultSet on a background thread (i.e. within the doInBackground() method). Call SwingWorker's publish method to publish Rows back to the Event Dispatch thread (e.g. every 100 rows).
  • When the SwingWorker's process method is called with the latest chunk of Rows read, add them to your List<Row> and fire appropriate TableEvents to cause the display to update.
  • Also, use the ResultSetMetaData to determine the Class of each column within the TableModel definition. This will cause them to be rendered correctly (which won't be the case if you simply use a 2D Object[][] array).

这种方法的优点是 UI 在处理大型 ResultSet 时不会锁定,并且显示会随着结果的处理而增量更新.

The advantage of this approach is that the UI will not lock up when processing large ResultSets, and that the display will update incrementally as results are processed.

编辑

在下面添加了示例代码:

Added example code below:

/**
 * Simple wrapper around Object[] representing a row from the ResultSet.
 */
private class Row {
  private final Object[] values;

  public Row(Object[] values) {
    this.values = values;
  }

  public int getSize() {
    return values.length;
  }

  public Object getValue(int i) {
    return values[i];
  }
}

// TableModel implementation that will be populated by SwingWorker.
public class ResultSetTableModel extends AbstractTableModel {
  private final ResultSetMetaData rsmd;
  private final List<Row> rows;

  public ResultSetTableModel(ResultSetMetaData rsmd) {
    this.rsmd = rsmd;
    this.rows = new ArrayList<Row>();
  }

  public int getRowCount() {
    return rows.size();
  }

  public int getColumnCount() {
    return rsmd.getColumnCount();
  }

  public Object getValue(int row, int column) {
    return rows.get(row).getValue(column);
  }

  public String getColumnName(int col) {
    return rsmd.getColumnName(col - 1); // ResultSetMetaData columns indexed from 1, not 0.
  }

  public Class<?> getColumnClass(int col) {
    // TODO: Convert SQL type (int) returned by ResultSetMetaData.getType(col) to Java Class.
  }
}

// SwingWorker implementation
new SwingWorker<Void, Row>() {
  public Void doInBackground() {
    // TODO: Process ResultSet and create Rows.  Call publish() for every N rows created.
  }

  protected void process(Row... chunks) {
    // TODO: Add to ResultSetTableModel List and fire TableEvent.
  }
}.execute();

这篇关于如何用数据库在JTable中填充数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆