我正在编写一个简单的javafx应用程序,它具有组合框,复选框,tableview控件。复选框文本为"仅显示简短的产品列表" 。我想,如果选中复选框,那么这将从h2数据库中获取所有数据,这是基于比较两个列和设置tableview的项目。例如,如果QTY列小于ShortPrd,则仅选择匹配的行。这就是我将数据推送到表的方式:
public void fillTable(ActionEvent actionEvent,String sqlQuery){
try {
System.out.println("Executing "+sqlQuery);
connection = SqlConnect.con();
preparedStatement = connection.prepareStatement(sqlQuery);
resultSet = preparedStatement.executeQuery();
PropertyValueFactory<TableData,String> namePro = new PropertyValueFactory<TableData,String>("name");
PropertyValueFactory<TableData,String> batchPro = new PropertyValueFactory<TableData,String>("batch");
PropertyValueFactory<TableData,String> expPro = new PropertyValueFactory<TableData,String>("exp");
PropertyValueFactory<TableData,Integer> qtyPro = new PropertyValueFactory<TableData,Integer>("qty");
//PropertyValueFactory<TableData,Double> mrpPro = new PropertyValueFactory<TableData,Double>("mrp");
PropertyValueFactory<TableData,Double> amtPro = new PropertyValueFactory<TableData,Double>("amt");
nameColumn.setCellValueFactory(namePro);
batchColumn.setCellValueFactory(batchPro);
expColumn.setCellValueFactory(expPro);
qtyColumn.setCellValueFactory(qtyPro);
//mrpColumn.setCellValueFactory(mrpPro);
amtColumn.setCellValueFactory(amtPro);
Hyperlink rbutton = new Hyperlink("Done");
while (resultSet.next()){
String name = resultSet.getString("name");
String batch = resultSet.getString("batch");
String exp = resultSet.getString("exp");
int qty = resultSet.getInt("qty");
Double mrp1 = resultSet.getDouble("mrp");
Double amt = resultSet.getDouble("rate");
tableData.add(new TableData(name,batch,exp,qty,mrp1,amt, rbutton));
}
tableView.setItems(tableData);
}
catch (SQLException e){
common.CustomException(e);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally {
try {
common.CloseCon();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
复选框事件处理程序:
shortCheckBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if(shortCheckBox.isSelected()){
onlyShortCheckBox.setDisable(true);
sqlQuery = "select * , CASE WHEN QTY<SHORTPRD THEN 'Only Display' ELSE 'No Display' END FROM ITEMS;";
tableView.getItems().clear();
fillTable(null,sqlQuery);
}
else{
onlyShortCheckBox.setDisable(false);
}
}
});
一切正常,只是我无法想到sql查询。
我尝试了基于条件的查询:
select * , CASE WHEN QTY<SHORTPRD THEN 'Only Display' ELSE 'No Display' END FROM ITEMS;
但是它显示了所有行,唯一的区别是带有额外的列。文本是基于条件的。这将是用户不想要的表格。这是截图:
获取仅匹配列的行的正确方法是什么?