Java 使用筛选器查找SQL数据
因此,我正在开发一个飞行管理系统,作为学校的一个项目,并且已经完成了一项任务 我必须让我的客户有机会搜索由直飞航班组成的多级航班 举个小例子:客户想要一个从德国到美国的航班,我有以下两个航班: 德国->土耳其 土耳其->美国 如果客户寻找多站航班,系统必须将两个航班合并为一个航班 到目前为止,我可以找到这两个航班,并将多级航班的名称保存到一个变量中,但我在查找航班时遇到了SQL语句的问题 到目前为止,我的代码是: 小说明:startAirportcombo是第一次飞行的起点选择工具。Zieltagetairportcombo是目标 查找第二个航班的方法:Java 使用筛选器查找SQL数据,java,sql,javafx,Java,Sql,Javafx,因此,我正在开发一个飞行管理系统,作为学校的一个项目,并且已经完成了一项任务 我必须让我的客户有机会搜索由直飞航班组成的多级航班 举个小例子:客户想要一个从德国到美国的航班,我有以下两个航班: 德国->土耳其 土耳其->美国 如果客户寻找多站航班,系统必须将两个航班合并为一个航班 到目前为止,我可以找到这两个航班,并将多级航班的名称保存到一个变量中,但我在查找航班时遇到了SQL语句的问题 到目前为止,我的代码是: 小说明:startAirportcombo是第一次飞行的起点选择工具。Zielta
möglicheFlüge =FXCollections.observableArrayList();
ResultSet myRs = pStatement.executeQuery(" select * from flügehin ");
while(myRs.next()) {
if (myRs.getString("startHinlughafen").equals(startAirportCombo.getValue())
&& myRs.getString("zielHinflughafen").equals(zielAirportCombo.getValue()) == false) {
tempMultiStopp = myRs.getString("zielHinflughafen");
if (myRs.getString("startHinlughafen").equals(tempMultiStopp) &&
myRs.getString("zielHinflughafen").equals(zielAirportCombo.getValue())) {
multiStop = tempMultiStopp;
}
}
}
M如何找到合并的航班并向客户展示:
if(seatsComboBusiness.getSelectionModel().isEmpty() ||seatsComboEco.getSelectionModel().isEmpty()) {
Alert alert = new Alert(Alert.AlertType.WARNING, "Bitte wählen Sie mindestens einen Sitzplatz aus!");
alert.setTitle("Fehler");
alert.showAndWait();
} else {
if (seatsComboBusiness.getValue() == 0 && seatsComboEco.getValue() == 0) {
Alert alert = new Alert(Alert.AlertType.WARNING, "Bitte wählen Sie mindestens einen Sitzplatz aus!");
alert.setTitle("Fehler");
alert.showAndWait();
} else {
try {
// Ablesen
Statement pStatement = connection.createStatement();
möglicheFlüge = FXCollections.observableArrayList();
ResultSet myRs = pStatement.executeQuery("select * from flügehin where startHinlughafen = \""
+ startAirportCombo.getValue() + "\" AND anzahlsitzplätzeeco >= \""
+ seatsComboEco.getValue() + "\" AND anzahlsitzplätzebus >= \""
+ seatsComboBusiness.getValue() + "\" AND zielHinFlughafen = \""
+ zielAirportCombo.getValue() + "\" AND ( hinflugdatum = \"" + date.getValue()
+ "\" OR hinflugdatum = \"" + date.getValue().plusDays(-1) + "\" OR hinflugdatum =\""
+ date.getValue().plusDays(-2) + "\" OR hinflugdatum =\"" + date.getValue().plusDays(-3)
+ "\" OR hinflugdatum =\"" + date.getValue().plusDays(1) + "\" OR hinflugdatum =\""
+ date.getValue().plusDays(2) + "\" OR hinflugdatum =\"" + date.getValue().plusDays(3)
+ "\" ) "
);
while (myRs.next()) {
möglicheFlüge.add(
new InstancedFlightModel(myRs.getInt("flightHinId"), myRs.getString("startHinlugHafen"),
myRs.getString("zielHinFlughafen"), myRs.getString("hinflugdatum"),
myRs.getString("flugzeug"), myRs.getInt("anzahlsitzplätzeeco"),
myRs.getInt("anzahlsitzplätzebus"), myRs.getString("uhrzeitHinflug"),
myRs.getFloat("preisEconomy"), myRs.getFloat("preisBusiness"),
myRs.getFloat("distanz"), myRs.getString("FGM"), secondDirectFlight()));
}
} catch (Exception e) {
Alert alert = new Alert(Alert.AlertType.ERROR,
"Bitte überprüfen Sie Ihre Angaben auf Vollständigkeit");
alert.setTitle("Kein Flug gefunden");
alert.showAndWait();
e.printStackTrace();
}
startAirport.setCellValueFactory(new PropertyValueFactory<>("startToAirport"));
multistoppColumn.setCellValueFactory(new PropertyValueFactory<>("multistopp"));
targetAirport.setCellValueFactory(new PropertyValueFactory<>("targetToAirport"));
flightDate.setCellValueFactory(new PropertyValueFactory<>("flightDate"));
flightTime.setCellValueFactory(new PropertyValueFactory<>("flightTime"));
airplane.setCellValueFactory(new PropertyValueFactory<>("airPlane"));
priceEco.setCellValueFactory(new PropertyValueFactory<>("priceEconomy"));
priceBus.setCellValueFactory(new PropertyValueFactory<>("priceBusiness"));
idColumn.setCellValueFactory(new PropertyValueFactory<>("flightNumber"));
airline.setCellValueFactory(new PropertyValueFactory<>("fluggesellschaft"));
distanz.setCellValueFactory(new PropertyValueFactory<>("distance"));
table.setItems(möglicheFlüge);
if (möglicheFlüge.isEmpty()) {
Alert alert = new Alert(Alert.AlertType.WARNING,
"Es konnte auf Basis Ihrer Eingaben kein passender Flug gefunden werden.");
alert.setTitle("Kein Flug gefunden");
alert.showAndWait();
}
}
}
我认为问题在于最后一个方法中的SQL语句,但multistop变量最终保持为null。所以也许有人可以帮我或者给我一个指针?这样我就可以解决航班不正常的问题。但现在的问题是,多次停止总是改变的。我有以下三个航班 德国-美国 德国-土耳其 土耳其-美国 德国-中国 现在,如果客户搜索德国-美国,即使没有从中国到美国的航班,Multistop也会变成中国。这是查找其他航班的方法的升级版本。我不得不重命名resultset来解决我的第一个问题
Statement pStatement = connection.createStatement();
ResultSet myRs2 = pStatement.executeQuery("select * from flügehin "
);
while (myRs2.next()) {
if (myRs2.getString("startHinlughafen").equals(startAirportCombo.getValue())
&& myRs2.getString("zielHinflughafen").equals(zielAirportCombo.getValue()) == false) {
tempMultiStopp = myRs2.getString("zielHinflughafen");
}
else {
tempMultiStopp = null;
}
if (myRs2.getString("startHinlughafen").equals(tempMultiStopp)
&& myRs2.getString("zielHinflughafen").equals(zielAirportCombo.getValue())) {
multiStop = tempMultiStopp;
}
}
您也可以使用SQL来执行此操作。我假设表名为flight,请参见下面的SQL。将源和目标的搜索条件传递到此SQL。它将查找连接为0或1的航班
SELECT *
FROM (
select origin, destination
from flight
where origin = ?
) a INNER JOIN
(
select origin, destination
from flight
where destination = ?
) b
ON a.destination = b.origin
;
对sql语句创建的简短反馈:在可能的情况下,您应该始终使用准备好的语句,而不是字符串连接!字符串连接打开了sql注入的应用程序。嘿,谢谢你的反馈,我对sql和java非常陌生,这就是为什么我真的不知道最好的方法。现在我只是想在学习的同时完成我的学校项目,但是如果你原来的问题得到了回答,你有了新问题,我会记住的,试着自己花点时间解决它。如果你花了一段时间后还不能解决这个问题,那就提出一个新问题。StackOverflow不是为处理动态问题而设计的。