Vaadin :将新项目提交到数据库(SQLContainer)

发布于 2024-12-27 20:46:04 字数 1207 浏览 2 评论 0 原文

我有一个显示图表的应用程序,其中填充了 SQL 表中的数据。我正在寻找一种可能性,让用户“编辑”表格,以便他可以更改图表。今天我发现了 Vaadin“SQLContainer”插件,这正是我所需要的。我能够连接到数据库并获取我需要的表,并将其连接到 Vaadin 表,这样我就可以看到 Vaadin 内的数据库表。我已经读过 Vaadin SQLContainer 教程(更新的 AdressBook 教程)好几次了,但我仍然不知道如何通过 SQLContainer 将某些内容提交到数据库。这是我到目前为止所得到的:

public void displayTable(){ 
    try {
        connectionPool = new SimpleJDBCConnectionPool(
                "org.postgresql.Driver",
                "jdbc:postgresql://localhost:5432/database", "username", "password", 2, 5);
        FreeformQuery query = new FreeformQuery("select * FROM table", connectionPool);
        container = new SQLContainer(query);
        container.addListener(new QueryDelegate.RowIdChangeListener() {
            public void rowIdChange(RowIdChangeEvent event) {
                System.err.println("Old ID: " + event.getOldRowId());
                System.err.println("New ID: " + event.getNewRowId());
            }
        });
    } catch (SQLException e) {
          e.printStackTrace();
    }
    table= new Table("Table",container);
    table.setSelectable(true);
    table.addListener(this);
    window.addComponent(table);
    }

}

我正在使用 Vaadin 版本 6.6.6 并使用 PostgrSQL。

I have an Application which displays a Chart, which is filled with data from an SQL table. I was looking for an possibility to let the user "Edit" the table so he can change the Chart. Today I found the Vaadin "SQLContainer" Add-on which is exactly what I need. I was able to connect to the Database and get the table I need and connect it to a Vaadin Table so I get to see a Database table inside Vaadin. I've read the Vaadin tutorial for SQLContainer (Updated AdressBook Tutorial) quite a few times but I still don’t get how to commit something to the DB via the SQLContainer. This is what I’ve got so far:

public void displayTable(){ 
    try {
        connectionPool = new SimpleJDBCConnectionPool(
                "org.postgresql.Driver",
                "jdbc:postgresql://localhost:5432/database", "username", "password", 2, 5);
        FreeformQuery query = new FreeformQuery("select * FROM table", connectionPool);
        container = new SQLContainer(query);
        container.addListener(new QueryDelegate.RowIdChangeListener() {
            public void rowIdChange(RowIdChangeEvent event) {
                System.err.println("Old ID: " + event.getOldRowId());
                System.err.println("New ID: " + event.getNewRowId());
            }
        });
    } catch (SQLException e) {
          e.printStackTrace();
    }
    table= new Table("Table",container);
    table.setSelectable(true);
    table.addListener(this);
    window.addComponent(table);
    }

}

I'm working with Vaadin Version 6.6.6 and I use PostgrSQL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江城子 2025-01-03 20:46:04

我看到您的查询只是“select * FROM table”。如果是这种情况,您应该使用 TableQuery 而不是 FreeformQuery。 TableQuery 已经实现了所有读/写、排序、过滤等功能,适用于直接从单个 SQL 表填充容器的情况。

使用 TableQuery,您应该能够将 vaadin 表设置为可编辑模式 (setEditable),并能够直接操作数据库中的值,通过调用 container.commit() 来存储它们,除非它处于 writeThrough 和 autoCommit 模式。或者,您可以选取表格的一行(一项)并在 Vaadin 表单中对其进行编辑。

如果您需要使用 FreeformQuery(对于更复杂的查询,例如连接两个或更多表),您需要自己实现写入支持,如另一个答案中所述。有关这方面的简单示例,请查看 SQLContainer 演示应用程序的 DemoFreeformQueryDelegate 实现。

华泰

I see that your query is just "select * FROM table". If this is the case, you should use TableQuery instead of FreeformQuery. TableQuery already has all read/write, sorting, filtering, etc implemented for the case that you are populating the container directly from a single SQL table.

Using TableQuery, you should be able to set the vaadin table in editable mode (setEditable) and directly be able to manipulate values in the database, storing them by calling container.commit() unless it's put in writeThrough and autoCommit modes. Or you can take one row of the table (one Item) and edit it in a Vaadin Form.

If you need to use FreeformQuery (for more complex queries, e.g. JOINing two or more tables) you need to implement write support yourself, as already stated in another answer. For a simple example on this, check out the implementation of the DemoFreeformQueryDelegate for the SQLContainer demo applications.

HTH

自由如风 2025-01-03 20:46:04

当谈到SqlContainer时,作者为您提供了以下信息

FreeformQuery 模式允许您指定任何复杂的查询并具有
它的结果填充容器,但是你需要执行
支持写入、排序、过滤和延迟加载
实现 FreeformQueryDelegate 接口。

我自己没有使用过 SqlContainer,但这里有一个 示例源代码它的。如果您按表中的一行,您将获得保存/编辑数据的选项。

When it comes to the SqlContainer the author provides you with he following information

FreeformQuery mode allows you to specify any complex query and have
it's results populate the container, however you need to impelment
support for writing, sorting, filtering and lazy loading by
implementing the FreeformQueryDelegate interface.

I haven't used the SqlContainer myself but here is an sample and the source code for it. If you press a row in the table you will get an option to save/edit data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文