gpt4 book ai didi

java - 如何使用 ComboBox 刷新与 SQLContainer 相关的 Vaadin 表?

转载 作者:行者123 更新时间:2023-11-30 04:44:49 25 4
gpt4 key购买 nike

我需要有关基于 Vaadin 的简单应用程序的帮助。

我需要一个与 SQL 查询结果绑定(bind)的表。 SQL 查询有一个参数,用户可以从组合框中选择该参数的值。我需要的是当用户更改组合框值时刷新表格。

这就是我所拥有的():

 Table table;
JDBCConnectionPool pool;
String query = "select products.product_code, products.name as product_name, clients.client_code, clients.name as client_name from products, clients where products.client_id = clients.id";

FreeformQuery q = new FreeformQuery(query, pool);
SQLContainer container = new SQLContainer(q);
table.setContainerDataSource(container);

因此,这个简单的代码从产品和客户表中选择所有数据并将其放入表中。但是,例如,如何通过从组合框中选择的clients.client_id 添加过滤?要实现下一个查询:

 select products.product_code, products.name as product_name, clients.client_code,  clients.name as client_name from products, clients where products.client_id = clients.id where client_id = ?;

感谢您的帮助。

最佳答案

您可以添加一个 Property.ValueChangeListener 来更改您的查询参数:

comboBox.addListener(new Property.ValueChangeListener() {
public void valueChange(ValueChangeEvent event) {
String customQuery = query.replace(":clientId", ((Client)(event.getProperty()).getId(), pks);
table.setContainerDataSource(new SQLContainer(new FreeformQuery(customQuery, pool)));
}
});

查询将包含以下值:select products.product_code, products.name as product_name,clients.client_code,clients.name as client_name from products,clients where products.client_id = client.id 其中 client_id = :clientId

但是要小心query.replace,如果Id是int则没什么好担心的,但是如果是string,请addSlashes 以避免 SQLInjection。

关于java - 如何使用 ComboBox 刷新与 SQLContainer 相关的 Vaadin 表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11277445/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com