gpt4 book ai didi

java - 根据用户输入在 PreparedStatement 中使用 setTime() 或 setNull()

转载 作者:行者123 更新时间:2023-11-29 12:10:49 29 4
gpt4 key购买 nike

我正在我的 JAVA 应用程序和 PostgreSQL 数据库中进行过滤搜索。

我有以下用于选择航类数据的 PostgreSQL 查询:

String query =  "SELECT flight.flight_id, route.from_location, route.to_location, flight.base_cost, flight.departure_date, flight.departure_time, flight.arrival_date, flight.arrival_time, aircraft.manufacturer, aircraft.seats " +
"FROM flight " +
"INNER JOIN route ON flight.route_id = route.route_id " +
"INNER JOIN aircraft ON flight.aircraft_id = aircraft.aircraft_id " +
"WHERE " +
"(from_location = CASE WHEN ? IS NULL THEN from_location ELSE ? END) AND " +
"(to_location = CASE WHEN ? IS NULL THEN to_location ELSE ? END) AND " +
"(base_cost = CASE WHEN ? IS NULL THEN base_cost ELSE ? END) AND " +
"(departure_date = CASE WHEN ? IS NULL THEN departure_date ELSE ? END) AND " +
"(departure_time = CASE WHEN ? IS NULL THEN departure_time ELSE ? END) AND " +
"(arrival_date = CASE WHEN ? IS NULL THEN arrival_date ELSE ? END) AND " +
"(arrival_time = CASE WHEN ? IS NULL THEN arrival_time ELSE ? END)";

然后我有这样的 Java 代码:

    ResultSet rs = null;
PreparedStatement prepStatement = connection.prepareStatement(query);
try {
if (!showFromTxtField.getText().trim().isEmpty()) {
prepStatement.setString(1, showFromTxtField.getText());
prepStatement.setString(2, showFromTxtField.getText());
} else {
prepStatement.setNull(1, java.sql.Types.VARCHAR );
prepStatement.setNull(2, java.sql.Types.VARCHAR );
}
if (!showToTxtField.getText().trim().isEmpty()) {
prepStatement.setString(3, showToTxtField.getText());
prepStatement.setString(4, showToTxtField.getText());
} else {
prepStatement.setNull(3, java.sql.Types.VARCHAR );
prepStatement.setNull(4, java.sql.Types.VARCHAR );
}
if (!showCostTxtField.getText().trim().isEmpty()) {
prepStatement.setFloat(5, Float.parseFloat(showCostTxtField.getText()));
prepStatement.setFloat(6, Float.parseFloat(showCostTxtField.getText()));
} else {
prepStatement.setNull(5, java.sql.Types.FLOAT );
prepStatement.setNull(6, java.sql.Types.FLOAT);
}
LocalDate depDate = showDepDateDatePicker.getValue();
if (depDate != null) {
prepStatement.setDate(7, Date.valueOf(depDate));
prepStatement.setDate(8, Date.valueOf(depDate));
} else {
prepStatement.setNull(7, java.sql.Types.DATE );
prepStatement.setNull(8, java.sql.Types.DATE);
}
if (!showDepTimeTxtField.getText().trim().isEmpty()) {
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
try {
Time timeValue = new Time(formatter.parse(showDepTimeTxtField.getText()).getTime());
prepStatement.setTime(9, timeValue);
prepStatement.setTime(10, timeValue);
} catch (ParseException e) {
e.printStackTrace();
}
} else {
prepStatement.setNull(9, java.sql.Types.TIME );
prepStatement.setNull(10, java.sql.Types.TIME );
}
LocalDate arrDate = showArrDatePicker.getValue();
if (arrDate != null) {
prepStatement.setDate(11, Date.valueOf(arrDate));
prepStatement.setDate(12, Date.valueOf(arrDate));
} else {
prepStatement.setNull(11, java.sql.Types.DATE );
prepStatement.setNull(12, java.sql.Types.DATE);
}
if (!showArrTimeTxtField.getText().trim().isEmpty()) {
DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
try {
Time timeValue = new Time(formatter.parse(showArrTimeTxtField.getText()).getTime());
prepStatement.setTime(13, timeValue);
prepStatement.setTime(14, timeValue);
} catch (ParseException e) {
e.printStackTrace();
}
} else {
prepStatement.setNull(13, java.sql.Types.TIME );
prepStatement.setNull(14, java.sql.Types.TIME );
}

rs = prepStatement.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}

所以重点是只返回那些符合文本字段标准的航类。如果任何文本字段留空,则查询应返回符合该特定条件的所有航类,但会使用其他非空条件进行过滤。

我在参数 $9 处出错。当我将输入到 showDepTimeTxtField 中的 String(例如“18:00:00”)转换为 java.sql.Time 对象时,我可以用作 prepStatement 方法 setTime() 的输入参数 我的查询应该只返回 departure_time 与输入时间相同的航类(满足其他条件)进入文本字段。如果文本字段留空且没有输入 String,我的查询应该从我的数据库中返回带有任何 departure_time 的航类。

我的问题是,当我运行我的代码时,出现以下错误:org.postgresql.util.PSQLException: ERROR: could not determine data type of parameter $9 我得到 wheter 我填充 departureTimeTxtField 与字符串或将其留空所以参数 departure_time 应设置为 NULL,我的查询应返回包含所有 departure_time 的航类。

所以我假设我在 setTime() 方法参数 timeValue 和从 Stringjava.sql 的转换方面有问题。时间

如何进行正确的转换以消除该错误?

编辑:更新了 SQL 查询和 Java 源代码。

最佳答案

我能够通过使用 COALESCE() 而不是 CASE WHEN ... 来解决这个问题:

String query = "SELECT * FROM flights WHERE departure_time = COALESCE(?, departure_time)";

关于java - 根据用户输入在 PreparedStatement 中使用 setTime() 或 setNull(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36379212/

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