我在mySQL数据库的列中重命名特定值时遇到问题。起初我以为我可以使用'AS'重命名,但我实际上是在尝试重命名列中的值。我的专栏名为FoundLost。在此列中,我存储值'0'和'1'。找到'0'并且'1'是迷失的。
我需要重命名此值的原因是因为我使用此数据库中的数据来创建pieChart。使用函数.getName,它给出了名称'0'和'1'。
我希望有人可以帮助我!
带有查询的类是以下代码:
public static ObservableList getPChartFoundLost() {
String query = "SELECT FoundLost, concat(round(count(FoundLost) *100 / (SELECT count(FoundLost) FROM Luggage))) AS percent FROM Luggage GROUP BY FoundLost";
ObservableList FoundLost = FXCollections.observableArrayList();
Connection connection = DatabaseUtils.connect();
if (connection != null) {
try {
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
FoundLost.add(new PieChart.Data(resultSet.getString("FoundLost"), resultSet.getInt("percent")));
}
resultSet.close();
statement.close();
} catch (SQLException sqle) {
System.out.println(sqle.getMessage());
}
DatabaseUtils.disconnect(connection);
}
return FoundLost;
}
控制器:
public void clickPChartFoundLost(ActionEvent event) {
//PieChart
ObservableList FoundLost = StatisticsUtils.getPChartFoundLost();
pieChart.setVisible(true);
pieChart.setData(FoundLost);
pieChart.setTitle("Found and Lost luggage");
for (final PieChart.Data data : pieChart.getData()) {
data.getNode().addEventHandler(MouseEvent.ANY,
new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent e) {
//Label vullen met data van Observable list uit Database
pieChartValueLable.setText(String.valueOf(data.getName()) + ": "
+ String.valueOf(data.getPieValue()) + "%");
}
});
}
}
谢谢!
答案 0 :(得分:1)
在您的查询中,请将此作为第一列,而不仅仅是FoundLost
。它会将您的0
和1
值转换为图表的有意义字符串。您的查询的其余部分可以保持不变。
IF(FoundLost = 0,'Found','Lost') AS FoundLost