我在基于Vaadin的简单应用程序中需要帮助。
我需要一个绑定到SQL查询结果的表。 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);
因此,这个简单的代码选择产品和客户表中的所有数据并将其放入表中。但是,如何通过从combobox中选择的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 = ?;
感谢您的帮助。
答案 0 :(得分:2)
您可以添加一个会更改查询参数的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)));
}
});
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 where client_id = :clientId
但请注意query.replace
,如果Id为int
,则无需担心,但如果是字符串,请addSlashes
以避免SQLInjection。