根据用户输入

时间:2016-04-02 21:59:28

标签: java sql postgresql jdbc prepared-statement

我正在我的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时出错。 当我将String(例如“18:00:00”)输入showDepTimeTxtField时转换为java.sql.Time对象,我可以将其用作prepStatement方法{{的输入参数1}}我的查询应仅返回与setTime()相同的航班(符合其他条件),与在文本字段中输入的时间相同。如果文本字段为空而没有输入departure_time,则我的查询应该从我的数据库返回包含String的任何航班。

我的问题是,当我运行我的代码时,我收到以下错误: org.postgresql.util.PSQLException:错误:无法确定参数$ 9 的数据类型,我将departure_time填入字符串或将其留空以便参数departureTimeTxtField应设置为NULL,我的查询应返回包含所有departure_time的广告。

所以我认为我的departure_time方法参数setTime()以及从timeValueString的转换存在问题。

如何进行正确的转换,以便我摆脱那种错误?

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

2 个答案:

答案 0 :(得分:2)

我可以使用COALESCE()代替CASE WHEN ...解决此问题:

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

答案 1 :(得分:0)

由于SQL中的CASE语法无效而产生错误(而不是因为时间格式),请查看CASE语法here

此外,从示例中,您似乎正在尝试获取所有在文本字段中出发时间为空或时间的航班,在这种情况下,我建议采用以下方法:

ResultSet rs = null;
PreparedStatement prepStatement = null;
if (!departureTimeTxtField.getText().trim().isEmpty()) {
    DateFormat formatter = new SimpleDateFormat("HH:mm:ss");
    Time timeValue = new Time(formatter.parse(departureTimeTxtField.getText()).getTime());
    prepStatement  = connection.prepareStatement("SELECT * FROM flights WHERE departure_time = ?");
    prepStatement.setTime(1, timeValue);
}else{
    prepStatement  = connection.prepareStatement("SELECT * FROM flights WHERE departure_time IS NULL");
}
rs = prepStatement.executeQuery();