Wangyanyixiang_branch
Lin 2 months ago
parent 4166439970
commit 26612cb0f4

@ -10,7 +10,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSession;
import javabean.db_conn; import javabean.db_conn;
import javabean.flight; import javabean.flight;
@ -32,35 +31,51 @@ public class search_fly extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8"); req.setCharacterEncoding("utf-8");
String url=null; String url = "default/index.jsp";
url="default/index.jsp";
String departure=null;
String destination=null;
departure=req.getParameter("departure");
destination=req.getParameter("destination");
String sql=null;
String departure = req.getParameter("departure");
String destination = req.getParameter("destination");
String departureYear = req.getParameter("departureYear");
String departureMonth = req.getParameter("departureMonth");
String departureDay = req.getParameter("departureDay");
String sql = null;
if(departure==""&&destination=="") { if (departure.isEmpty() && destination.isEmpty() && departureYear.isEmpty() && departureMonth.isEmpty() && departureDay.isEmpty()) {
//如果始发地和目的地都为空则跳转回原页面 // 如果所有字段都为空,重定向到原页面
resp.sendRedirect(url); resp.sendRedirect(url);
}else if (departure!=""&&destination=="") { } else {
sql="select * from flight where f_s_p='"+departure+"'"; sql = "SELECT * FROM flight WHERE 1=1";
}else if (departure==""&&destination!="") {
sql="select * from flight where f_a_p='"+destination+"'"; if (!departure.isEmpty()) {
}else if (departure!=""&&destination!="") { sql += " AND f_s_p='" + departure + "'";
sql="select * from flight where f_s_p='"+departure+"' and f_a_p='"+destination+"'";
} }
if(departure==""&&destination=="") { if (!destination.isEmpty()) {
}else { sql += " AND f_a_p='" + destination + "'";
db_conn conn=new db_conn(); }
if (!departureYear.isEmpty() && !departureMonth.isEmpty() && !departureDay.isEmpty()) {
// 验证日期格式
try {
int year = Integer.parseInt(departureYear);
int month = Integer.parseInt(departureMonth);
int day = Integer.parseInt(departureDay);
if (year >= 1900 && year <= 9999 && month >= 1 && month <= 12 && day >= 1 && day <= 31) {
String departureDate = String.format("%04d-%02d-%02d", year, month, day);
sql += " AND DATE(f_Date)='" + departureDate + "'";
} else {
req.setAttribute("errorMsg", "日期格式不正确,请输入有效的年、月、日");
}
} catch (NumberFormatException e) {
req.setAttribute("errorMsg", "日期格式不正确,请输入数字");
}
}
db_conn conn = new db_conn();
ArrayList<flight> flightlist = new ArrayList<flight>(); ArrayList<flight> flightlist = new ArrayList<flight>();
ResultSet res=conn.executeQuery(sql); ResultSet res = conn.executeQuery(sql);
try { try {
while (res.next()) { while (res.next()) {
flight flight_info=new flight(); flight flight_info = new flight();
flight_info.setF_n(res.getString(1)); flight_info.setF_n(res.getString(1));
flight_info.setF_s_p(res.getString(2)); flight_info.setF_s_p(res.getString(2));
flight_info.setF_a_p(res.getString(3)); flight_info.setF_a_p(res.getString(3));
@ -70,16 +85,14 @@ public class search_fly extends HttpServlet {
flight_info.setF_a_t(res.getString(7)); flight_info.setF_a_t(res.getString(7));
flight_info.setF_p(res.getString(8)); flight_info.setF_p(res.getString(8));
flightlist.add(flight_info); flightlist.add(flight_info);
} }
req.setAttribute("flightlist", flightlist); req.setAttribute("flightlist", flightlist);
} catch (SQLException e) { } catch (SQLException e) {
System.out.println("错误信息:"+e); System.out.println("错误信息:" + e);
}finally { } finally {
conn.closeDB(); conn.closeDB();
} }
req.getRequestDispatcher("default/search.jsp").forward(req, resp); req.getRequestDispatcher("default/search.jsp").forward(req, resp);
} }
} }
} }

Loading…
Cancel
Save