我正在尝试基于几个参数来实现对数据库元素的搜索。为此,我写了这个请求:
public List<Flight> findFlightByParametrs(String startPoint, String endPoint, String date)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_PARAMETRS);
pstmt.setInt(1, Integer.parseInt(startPoint));
pstmt.setInt(2, Integer.parseInt(endPoint));
pstmt.setInt(3, Integer.parseInt(date));
rs = pstmt.executeQuery();
while (rs.next()) {
list.add(getFlight(rs));
}
con.commit();
} catch (SQLException e) {
rollback(con);
throw new DBException("Cannot find flight by parametrs", e);
} finally {
close(con, pstmt, rs);
}
return list;
}
我还制作了一个jsp页面,其中有三个用于输入参数的字段和一个按钮,我应该在该按钮上显示数据库中的项目。
<from action="controller" method="post">
<label for="sp"><fmt:message key="label_sp"/></label>
<input type="text" id="sp" placeholder="<fmt:message key="find_sp"/>" name="startPoint">
<label for="ep"><fmt:message key="label_ep"/></label>
<input type="text" id="ep" placeholder="<fmt:message key="find_ep"/>" name="endPoint">
<label for="date"><fmt:message key="label_date"/></label>
<input type="text" id="date" placeholder="<fmt:message key="find_date"/>" name="departureDate">
<input type="hidden" name="command" value="getFlights">
<input type="submit" value="<fmt:message key="flight_search"/>">
</from>
还有一个类,我在其中查询参数并检查它们是否不为空。
String startPoint = request.getParameter("startPoint");
String endPoint = request.getParameter("endPoint");
String departureDate = request.getParameter("departureDate");
if (startPoint != null && endPoint != null && departureDate!= null){
flight = db.findFlightByParametrs(startPoint, endPoint, departureDate);
}
当我单击按钮时,什么也没有发生,我无法从数据库中找到项目。我在哪里弄错了?我将很感谢您的帮助,以下是我的sql请求。
private static final String GET_FLIGHT_BY_PARAMETRS = "SELECT * FROM flight where startPoint=? AND endPoint=? AND departure_date=?";