Vaadin :将新项目提交到数据库(SQLContainer)
我有一个显示图表的应用程序,其中填充了 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。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到您的查询只是“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
当谈到SqlContainer时,作者为您提供了以下信息
我自己没有使用过 SqlContainer,但这里有一个 示例 和 源代码它的。如果您按表中的一行,您将获得保存/编辑数据的选项。
When it comes to the SqlContainer the author provides you with he following information
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.