数据库到GlazedList / Jtable,然后通过GlazedList / JTable编辑数据库 [英] Database to GlazedList/Jtable and then edit the database through the GlazedList/JTable

查看:121
本文介绍了数据库到GlazedList / Jtable,然后通过GlazedList / JTable编辑数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够将这个问题分解为两个问题:


  1. 放置数据库内容的最佳方法是什么( MS-Access)进入 GlazedList / JTable

  2. 如何确保对 GlazedList / JTable 会反映在数据库中(MS-Access)?

以下是我所知道的事情:


  1. 我知道如何检索/使用
    从数据库中操作
    信息 JDBC方法

  2. 我知道 GlazedList
    需要反射,所以我需要
    来创建一个包含每个
    列/字段的类数据库。这个
    不是很容易扩展......

解决这个问题的最佳方法是什么?



编辑:// 我设法创建了一个类生成器。它采用列标题并创建实例字段。这应解决#2
http://pastebin.ca/1770996 - 它会创建课程但是我觉得我没有正确地使用反射...
edit2:// 从上面编辑我的代码所以它有效... http://pastebin.ca/1776722

解决方案

我有一个非常相似的问题,我认为我的结果也是类似的,除了它不需要反射(静态数据库模式)。您需要为每一行创建行对象(可能只包括行号和对ResultSet和列信息的引用)。



然后写一个 ca.odell.glazedlists.gui.WritableTableFormat 实现,将这些对象映射到表格单元格。



为避免#2出现问题,您可以创建一个灵活的行类,从ResultSet中提取一次列信息并缓存它以供重用。



编辑:我找到了一个原始的,更简单的实现(相当简单),我的基础。您可以在此处查看: ResultSet表。它可能足以满足您的目的。然后将其添加到链接提供的AbstractTableModel实现中。

  public void setValueAt(Object ob,int row,int column)抛出SQLException {
resultSet.absolute(r + 1);
if(ob == null){
resultSet.updateNull(column + 2);
} else {
resultSet.updateObject(column + 2,ob);
}
rs.updateRow();
this.fireTableCellUpdated(row,column);
}
public boolean isCellEditable(int row,int col){
return true;
}

虽然有三个捕获:你的ResultSet需要是可更新的,支持同时滚动方向,并对数据库的更新敏感。这些是JDBC规范的一部分,但并非所有驱动程序都支持它们,并且您需要确保在启用它们的情况下创建ResultSet。在这种情况下,您只需定期执行 this.fireTableDataChanged()来强制完全更新表数据。这不是最快的方法,但确实有效。






编辑2:另一种方法



如何使用其中一个Object-relational映射器库,然后执行上面建议的 ca.odell.glazedlists.gui.WritableTableFormat ? / p>

I am able to break this problem down into two questions:

  1. What is the best way to put the contents of a database (MS-Access) into a GlazedList/JTable?
  2. How do I make sure any changes made to the GlazedList/JTable are reflected on the database (MS-Access)?

Here are the things I know:

  1. I know how to retrieve/manipulate the information from a database using the JDBC method.
  2. I know that GlazedList's require reflection so I would need to make a class that contains every column/field in the database. This is not very expandable...

What is the best way to go about this problem?

edit:// I have managed to create a class generator. It takes the column headings and creates an instance field. This should resolve the #2 http://pastebin.ca/1770996 - It creates the class but I do not think I used reflection correctly... edit2:// Edited my code from above so it works... http://pastebin.ca/1776722

解决方案

I had a very similar problem, and I think my result was similar too, except it didn't need reflection (static DB schema). You need to create row objects for each row (which may just include row number and references to a ResultSet and column info).

Then write a ca.odell.glazedlists.gui.WritableTableFormat implementation to map these objects to table cells.

To avoid problems with #2, you can create a flexible row class that fetches column info once from the ResultSet and caches it for reuse.

Edit: I found an original and simpler implementation (fairly simple) that mine was based upon. You can view it here: ResultSet Table. It might be sufficient for your purposes. Then you add this to the AbstractTableModel implementation provided by the link.

public void setValueAt(Object ob, int row, int column) throws SQLException {
    resultSet.absolute(r+1);
    if (ob == null) {
        resultSet.updateNull(column+2);
    } else {
        resultSet.updateObject(column+2,ob);
    }
    rs.updateRow();
    this.fireTableCellUpdated(row,column);  
}
public boolean isCellEditable(int row, int col) {
    return true;
}

There are three catches though: your ResultSet needs to be updatable, support scrolling both directions, and be sensitive to updates to the DB. These are part of the JDBC spec, but not all drivers support them, and you need to make sure your ResultSet is created with them enabled. In that case, you just do this.fireTableDataChanged() periodically to force a full update of the table data. It's not the fastest approach, but it does work.


Edit2: Another approach

What about using one of the Object-relational mapper libraries, and then do the ca.odell.glazedlists.gui.WritableTableFormat like I suggested above?

这篇关于数据库到GlazedList / Jtable,然后通过GlazedList / JTable编辑数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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