由于自身技术限制,目前只写出了对线路的查询和查出同一线路的信息,对于换乘问题还没有解决,接下来就谈谈我的理解。
List<Subway> subwayn = dao.getSubwayByNumber(number); 1
if(subwayn == null) { req.setAttribute("info", "无此路线!"); req.getRequestDispatcher("road.jsp").forward(req,resp); } else { req.setAttribute("subwayn", subwayn); 22中的subwayn是获取1中的subwayn的值,2中的"subwayn"是设置一个节点,将subwayn的值赋予"subwayn",且"subwayn"需要与jsp中<c:forEach items="${subwayn}" var="item" varStatus="status">中的加红字体一致。
查询线路信息:
public List<Subway> getSubwayByNumber(int number) {
String sql = "select * from subwayn where number = '" + number + "'"; List<Subway> list = new ArrayList<>(); Connection conn = Util.getConnection(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Subway subwayn = null; while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String info = rs.getString("info"); subwayn = new Subway(id, number, name, info); list.add(subwayn); }通过id查找同一线路的信息,并将其打印出来。
途经信息查询:
private void getSubwayByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{
req.setCharacterEncoding("utf-8"); String name1 = req.getParameter("name1"); String name2 = req.getParameter("name2"); Subway subwayn1 = dao.getSubwayByName(name1); Subway subwayn2 = dao.getSubwayByName(name2); List<Subway> subwayn3=new ArrayList<>(); subwayn3 = dao.search(name1,name2); int m = 0; if(subwayn1.getNumber()==subwayn2.getNumber()){ m = Math.abs(subwayn1.getId()-subwayn2.getId()); }name1是起点站名,name2是终点站名,m为两站点id值得差表示途径几站,subwayn3为新建一个表,将途经站点信息储存。
String sql3 = "select * from subwayn where id between '" + subwayn1.getId() + "' and '" + subwayn2.getId() + "'";
查询id值在subwayn1和subwayn2之间的站点
还有就是通过对数据库的学习,我对SELECT语句的理解和运用更加深刻了。
对于换乘的问题,我们还将继续讨论。