通过数据数据库实现搜索字段

时间:2018-06-19 12:57:57

标签: java jsp servlets java-web-start

我有一个数据库,其中包含以下表格:航空公司航班(多对一航空公司)。

我正在尝试根据航班号进行航班搜索。

数据库表中每个字段的获取和设置在哪里?

public List<Flight> findFlightsByAircompany(int aircompany_id) throws DBException {
        List<Flight> list = new ArrayList<Flight>();
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            con = getConnection();
            pstmt = con.prepareStatement(GET_FLIGHT_BY_AIRCOMPANY);
            pstmt.setInt(1, aircompany_id);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                list.add(getFlight(rs));
            }
            con.commit();
        } catch (SQLException e) {
            rollback(con);
            throw new DBException("Cannot find flight by aircompany id", e);
        } finally {
            close(con, pstmt, rs);

        }
        return list;
    }

    public List<Flight> findFlightByNumber(String number) throws DBException{
        List<Flight> list = new ArrayList<Flight>();
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            con = getConnection();
            pstmt = con.prepareStatement(GET_FLIGHT_BY_NUMBER);
            pstmt.setInt(1, Integer.parseInt(number));
            rs = pstmt.executeQuery();
            while (rs.next()) {
                list.add(getFlight(rs));
            }
            con.commit();
        } catch (SQLException e) {
            rollback(con);
            throw new DBException("Cannot find flight by number", e);
        } finally {
            close(con, pstmt, rs);

        }
        return list;
    }

private Flight getFlight(ResultSet rs) throws SQLException, DBException {
        Flight flight = new Flight();
        flight.setId(rs.getInt("ID"));
        flight.setName(rs.getString(NAME));
        flight.setStartPoint(rs.getString("STARTPOINT"));
        flight.setEndPoint(rs.getString("ENDPOINT"));
        flight.setNumber(rs.getInt("NUMBER"));
        flight.setDepartureTime(rs.getInt("DEPARTURE_TIME"));
        flight.setAircompanyId(rs.getInt("AIRCOMPANY_ID"));
        return flight;
    }

SQL请求

private static final String GET_FLIGHT_BY_AIRCOMPANY = "SELECT * from flight where AIRCOMPANY_ID=?";    
private static final String GET_FLIGHT_BY_NUMBER = "SELECT * from flight where number=?";

之后,我尝试获取属性

int aircompany = -1;

    if (session.getAttribute(AIRCOMOANY) != null) {
                aircompany = (Integer) session.getAttribute(AIRCOMOANY);
            }

            if (request.getParameter(AIRCOMOANY) != null) {
                aircompany = Integer.valueOf(request.getParameter(AIRCOMOANY));
            }
            System.out.println(aircompany);

            String sort = request.getParameter("sort");

            List<Flight> flight = db.findFlightsByAircompany(aircompany);;

            if ("name".equals(sort)) {
                Collections.sort(flight, new Comparators.CompareByName());
            } else if ("number".equals(sort)) {
                Collections.sort(flight, new Comparators.CompareByNumber());
            }
            String search = request.getParameter("search");
            if ("number".equals(search)){
                System.out.println("fewfw");
                flight = db.findFlightByNumber(String.valueOf(session.getAttribute("number"))); //maybe it's wrong
            }

如何正确调用“数字”字段,然后在JSP页面上使用?

<div align="center">
    <form action="controller" method="post">
        <fmt:message key="sort_by"/> <select name="sort">
        <option value="name"><fmt:message key="admin_jsp.aircompany_name"/></option>
        <option value="number"><fmt:message key="number"/></option>
    </select> <input type="hidden" name="command" value="getFlights"> <input
            type="submit" value="<fmt:message key="sort"/>">
    </form>
    <br>
    <form action="controller" method="post">
        <option value ="number"><fmt:message key="number"/></option><input type="text" name="search" value ="">
        <input type ="hidden" name ="command" value ="getFlights"> <input type="submit" value="Search">
    </form>

</div>

我的应用程序的一些屏幕截图。

  • 在第一个屏幕上,选择航空公司,然后获得航班列表。

    App screenshot

  • 第二秒钟我必须按航班号搜索。为了清楚起见,在我的课堂上,我举了一个例子说明如何进行排序,它如何正确运行,但是通过搜索,我遇到了问题。我需要在字段中输入航班号并仅接收它。

    screenshot

1 个答案:

答案 0 :(得分:0)

您在哪里设置会话属性“ AIRCOMOANY”?这是在某处定义的常数吗?还要确保这不是引起您问题的拼写错误,因为 aircompany = Integer.valueOf(request.getParameter(AIRCOMOANY));可能引起一些问题。

flight = db.findFlightByNumber(String.valueOf(session.getAttribute("number")));

您正在使用session.getAttribute(“ number”),但在设置会话属性“ number”的位置上看不到任何地方。

如果您只希望用户输入航班号

`<form action="controller" method="post">
    <option value ="number"><fmt:message key="number"/></option>
    <input type="text" name="search" value ="">
    <input type ="hidden" name ="command" value ="getFlights">
    <input type="submit" value="Search">
</form>`

尝试类似的东西:

`<form action="controller" method="post">
    <label for="num">Flight Number</label>
    <input type="text" id="num" name="number">
    <input type ="hidden" name ="command" value ="getFlights">
    <input type="submit" value="Search">
</form>`

然后在您的控制器中,而不是:

`String search = request.getParameter("search");
if ("number".equals(search)){
    System.out.println("fewfw");
    flight = db.findFlightByNumber(String.valueOf(session.getAttribute("number")));
}`

尝试:

`String number = request.getParameter("number");
if(number != null){
    System.out.println("fewfw");
    flight = db.findFlightByNumber(number);
}`

祝你好运,希望这会有所帮助。