如何用数据库填充JTable中的数据?
我想显示一个 JTable,它按原样显示数据库表中的数据。
到目前为止,我已经使用 JTable 显示来自 Object [ ] [ ] 的数据。
我知道显示数据的一种方法是首先将数据库表转换为对象[][],但是有没有其他简单但更强大和灵活的方法。
I want to display a JTable that display the data from a DataBase table as it is.
Up till now, I have used JTable that displays data from 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我建议采用以下方法:
Row
类来表示从ResultSet
读取的行。这可以是Object[]
的简单包装。List|
集合,以及由该集合支持的子类AbstractTableModel
。SwingWorker
通过从后台线程(即在doInBackground()
方法中)。调用SwingWorker
的publish
方法将Row
发布回事件调度线程(例如每 100 行)。SwingWorker
的process
方法时,将它们添加到List|
并触发适当的 < code>TableEvents 导致显示更新。ResultSetMetaData
确定TableModel
定义中每列的Class
。这将使它们正确渲染(如果您仅使用 2DObject[][]
数组,则不会出现这种情况)。这种方法的优点是,在处理大型
ResultSet
时,UI 不会锁定,并且显示会随着结果的处理而逐渐更新。编辑
添加了以下示例代码:
I would recommend taking the following approach:
Row
class to represent a row read from yourResultSet
. This could be a simple wrapper around anObject[]
.List<Row>
collection, and subclassAbstractTableModel
to be backed by this collection.SwingWorker
to populate yourList<Row>
by reading from the underlyingResultSet
on a background thread (i.e. within thedoInBackground()
method). CallSwingWorker
'spublish
method to publishRow
s back to the Event Dispatch thread (e.g. every 100 rows).SwingWorker
'sprocess
method is called with the latest chunk of Rows read, add them to yourList<Row>
and fire appropriateTableEvent
s to cause the display to update.ResultSetMetaData
to determine theClass
of each column within theTableModel
definition. This will cause them to be rendered correctly (which won't be the case if you simply use a 2DObject[][]
array).The advantage of this approach is that the UI will not lock up when processing large
ResultSet
s, and that the display will update incrementally as results are processed.EDIT
Added example code below:
在 JTable 中显示数据库数据的另一种强大而灵活的方法是将查询的结果数据加载到 CachedRowSet,然后将其连接到 JTable 与 TableModel 适配器。
这本由 George Reese 编写的书给出了其类RowSetModel的源代码,以适应RowSet 作为 TableModel。开箱即用地为我工作。我唯一的改变是为该类起了一个更好的名称:RowSetTableModel。
RowSet 是 ResultSet 的子接口,在 Java 1.4 中添加。因此 RowSet 是一个 ResultSet。
CachedRowSet 实现会为您完成这项工作,而不是像本页其他答案中所讨论的那样创建 Row 类、Row 对象列表和 ResultSetMetaData。
Sun/Oracle 提供了 CachedRowSet 的参考实现。其他供应商或 JDBC 驱动程序也可能提供实现。
行集教程
Another powerful and flexible way to display database data in a JTable is to load your query's resulting data into a CachedRowSet, then connect it to the JTable with TableModel adapter.
This book by George Reese gives the source code for his class RowSetModel to adapt a RowSet as a TableModel. Worked for me out-of-the-box. My only change was a better name for the class: RowSetTableModel.
A RowSet is a subinterface of ResultSet, added in Java 1.4. So a RowSet is a ResultSet.
A CachedRowSet implementation does the work for you, instead of you creating a Row class, a List of Row objects, and ResultSetMetaData as discussed in other answers on this page.
Sun/Oracle provides a reference implementation of CachedRowSet. Other vendors or JDBC drivers may provide implementations as well.
RowSet tutorial
根据您已经完成的操作以及您愿意执行的操作,我一直在使用 Netbeans 及其对数据库驱动应用程序的 Beans 绑定支持,非常成功。您将 JTable 绑定到数据库,它会自动构建 JPA 查询。
Depending on what you've done already and what you're willing to do, I've been using Netbeans with its Beans Binding support for a database-driven app very successfully. You bind your JTable to a database and it automatically builds the JPA queries.
使用 ResultSet 填充 jTable 的最佳方法
先决条件
1) 结果集“rs”填充了您需要的数据。
2) JTable“jTable1”是预先创建的
3)表头预先
实现
Best way to fill jTable with ResultSet
Prerequisites
1) Result Set "rs" is populated with data you need.
2) JTable "jTable1" is created before hand
3) Table Header is implemented before hand
Implementation
您必须在那里创建一个自定义 TableModel您可以指定数据的来源和方式。
您确实必须首先完全理解 JTable + TableModel 工作,然后请遵循之前发布的答案之一。
You have to create a custom TableModel There you can specify where and how the data is coming from.
You really have to fully understand first how JTable + TableModel works and then follow one of the previously posted answers.
我知道这个问题已经很老了,但是对于遵循 Adamski 解决方案的任何人来说,在 gui 和
SwingWorker
线程之间共享ResultSet
和ResultSetMetadata
时应该小心。在 SQLite 中使用这种方法时,我遇到了不一致的内部状态异常。解决方案是在执行SwingWorker
之前将任何元数据加载到私有字段,并使用 getter 函数(getColumnName 等)来返回字段。I know the question is old but for anyone following Adamski's solution, care should be taken while sharing the
ResultSet
andResultSetMetadata
between gui andSwingWorker
threads. I got an inconsistent internal state exception while using this approach with SQLite. The solution is to load any metadata to private fields before executing theSwingWorker
and have the getter functions (getColumnName etc.) to return the fields instead.我给出了一个在 JTable 中显示数据库表数据的小方法。您只需传递数据库表的结果集作为参数。
I am giving a small method for display database table data in JTable. You need to pass only the resultset of the database table as parameter.