• 结对开发石家庄地铁线路


    对象:潘攀达

    思路:站点有两个属性,编号和站名,一号线的编号是1开头的,二号线的编号是2开头的,以此类推,可以换乘的站点便拥有多个编号。

    如果开头数相同,则两个站点在同一条路线不需要换乘,如果开头数不同则要进行换乘。所以获取起始站点的id号与终点站点的id号进行比较,如果他俩有相同的id号那么证明两战点在同一条线路上 则不需要换乘即可到达,如果两个站点没有相同的id号,那么说明不再一条线路上,需要换乘,则搜索终点站所拥有的id号是否有与起始站点id号相同的id号,这里的线路图一个换乘必能到达,所以肯定会有。根据两者的id号之间的联系,进行遍历的输出id号数组,根据id数组查出站名即可。

    在获取他们之间站点的id号,通过id号获取站点名字来实现线路信息。

    数据库设计:

     

    package Entity;
    
    public class Station {
    	private int id;
    	private String name;
    	
    	public int getId() {
    		return id;
    	}
    	public void setId(int id) {
    		this.id = id;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public Station(int id, String name) {
    		this.id = id ;
    		this.name = name;
    	}
    
    }
    

      

    package DB;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    
    public class DB {
    
    	public static String db_url = "jdbc:mysql://localhost:3306/info_s";
    	public static String db_user = "root";
    	public static String db_pass = "z376371066.";
    	
    	public static Connection getConn () {
    		Connection conn = null;
    		
    		try {
    			Class.forName("com.mysql.jdbc.Driver");//加载驱动
    			conn = DriverManager.getConnection(db_url, db_user, db_pass);
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    		return conn;
    	}
    	
    	/**
    	 * 关闭连接
    	 * @param state
    	 * @param conn
    	 */
    	public static void close (Statement state, Connection conn) {
    		if (state != null) {
    			try {
    				state.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    		
    		if (conn != null) {
    			try {
    				conn.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    	
    	public static void close (ResultSet rs, Statement state, Connection conn) {
    		if (rs != null) {
    			try {
    				rs.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    		
    		if (state != null) {
    			try {
    				state.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    		
    		if (conn != null) {
    			try {
    				conn.close();
    			} catch (SQLException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	public static void main(String[] args) throws SQLException {
    		Connection conn = getConn();
    		PreparedStatement pstmt = null;
    		ResultSet rs = null;
    		String sql ="select * from sjzsubway";
    		pstmt = conn.prepareStatement(sql);
    		rs = pstmt.executeQuery();
    		if(rs.next()){
    			System.out.println("空");
    		}else{
    			System.out.println("不空");
    		}
    	}
    
    }
    

      

    package Dao;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;
    
    import DB.DB;
    import Entity.Station;
    
    public class Dao {
    	public List<Station> getStationByName(String name) {
    		String sql = "select * from sjzsubway where name ='" + name + "'";
    		Connection conn = DB.getConn();
    		Statement state = null;
    		ResultSet rs = null;
    		
    		List<Station> list = new ArrayList<>();
    		Station bean = null;
    		try {
    			state = conn.createStatement();
    			rs = state.executeQuery(sql);
    			while (rs.next()) {
    				int id = rs.getInt("id");
    				bean = new Station(id,name);
    				list.add(bean);
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			DB.close(rs, state, conn);
    		}
    		
    		return list;
    	}
    	public ResultSet getStationById(int id) {
    		Connection conn = DB.getConn();
    		Statement state = null;
    		ResultSet rs = null;
    				
    		try {
    
    			String sql = "select * from sjzsubway where id  = '" + id + "'";
    			state = conn.createStatement();
    			rs = state.executeQuery(sql);
    			
    
    		 
    		} catch (Exception e) {
    			e.printStackTrace();
    		} 
    		 return rs;
    	}
    	public List<Station> getSameAll(int number) {
    		String sql = "select * from sjzsubway where id like'" + number + "__'";
    		Connection conn = DB.getConn();
    		Statement state = null;
    		ResultSet rs = null;
    		
    		List<Station> list = new ArrayList<>();
    		Station bean = null;
    		try {
    			state = conn.createStatement();
    			rs = state.executeQuery(sql);
    			while (rs.next()) {
    				String name  = rs.getString("name");
    				int id = rs.getInt("id");
    				bean = new Station(id,name);
    				list.add(bean);
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		} finally {
    			DB.close(rs, state, conn);
    		}
    		return list;
    	}
    	
    	
    
    }
    

      

    package Servlet;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import Dao.Dao;
    import Entity.Station;
    
    /**
     * Servlet implementation class StationServlet
     */
    @WebServlet("/StationServlet")
    public class StationServlet extends HttpServlet {
    	private static final long serialVersionUID = 1L;
        Dao dao = new Dao();
        /**
         * @see HttpServlet#HttpServlet()
         */
        public StationServlet() {
            super();
            // TODO Auto-generated constructor stub
        }
    
    	/**
    	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    		req.setCharacterEncoding("utf-8");
    		String method = req.getParameter("method");
    		if ("chaxun".equals(method)) {
    			chaxun(req,resp);
    		}
    	}
        
    	
    	private void chaxun(HttpServletRequest req, HttpServletResponse resp)throws IOException, ServletException {
    		// TODO Auto-generated method stub
    		req.setCharacterEncoding("utf-8");
    		String startname = req.getParameter("startname");
    		String endname = req.getParameter("endname");
    		List<Station> stations1 = dao.getStationByName(startname);
    		List<Station> stations2 = dao.getStationByName(endname);
    		int num = 0;
    		int[] rute = new int[20] ;
        	for(int m = 0; m < stations1.size();m++) {
        		for (int i = 0; i < stations2.size(); i++) {//不需要换乘
            		if(((stations2.get(i).getId())/100==(stations1.get(m).getId())/100)&&stations2.get(i).getId()>stations1.get(m).getId()) {
            			for(int j =stations1.get(m).getId();j<= stations2.get(i).getId();j++) {
            				rute[num] = j;
            				num++;
            			}
            		}
            		else if(((stations2.get(i).getId())/100==(stations1.get(m).getId())/100)&&stations2.get(i).getId()<stations1.get(m).getId()) {
            			for(int j =stations1.get(m).getId();j>= stations2.get(i).getId();j--) {
            				rute[num] = j;
            				num++;
            			}
            		}
            		else {//需要换乘一次
            			int numid1 = 0,numid2 = 0;
    					int[] id1 = new int [5];
    					int[] id2 = new int [5];
    					int sum=0;
    					int number = 0;
    					for(int xx = 0; xx < stations1.size();xx++) {
    						for(int x = 0; x < stations2.size();x++) {
    							number=stations2.get(x).getId()/100;
    							List<Station> samerute  = dao.getSameAll(number);//查询与终点站一条路线的所有站点的信息
    							for(int y = 0; y < samerute.size();y++) {
    								List<Station> samerute1  = dao.getStationByName(samerute.get(y).getName());
    								for(int z = 0; z < samerute1.size();z++) {
    									if((samerute1.get(z).getId()/100==stations1.get(xx).getId()/100)) {//查询终点线路上与起始站具有相同编码开头的站点名称,即中转站信息
    								   
    									String name = samerute1.get(z).getName();
    									List<Station> ids = dao.getStationByName(name);
    									for(int a = 0; a < ids.size();a++) {
    										if(ids.get(a).getId()/100==stations1.get(z).getId()/100) {
    											id1[numid1] = ids.get(a).getId();//与起始站编码开头相同的中转站的编号
    											numid1++;
    										}
    										if(ids.get(a).getId()/100==stations2.get(z).getId()/100) {
    											id2[numid2] = ids.get(a).getId();//与终点站编码开头相同的中转站的编号
    											numid2++;
    										}
    										
    									}
    										if(id1[0]>stations1.get(z).getId()) {
    											if(id2[0]>stations2.get(x).getId()) {
    												for(int p = stations1.get(xx).getId();p<id1[0];p++) {
    													rute[sum]=p;
    													sum++;
    													
    												}
    												for(int q =id2[0];q >=stations2.get(x).getId();q--  ) {
    													rute[sum]=q;
    													sum++;
    												}
    												
    												
    											}
    											else if(id2[0]<stations2.get(x).getId()) {
    												for(int p = stations1.get(xx).getId();p<id1[0];p++) {
    													rute[sum]=p;
    													sum++;
    												}
    												for(int q =id2[0];q <=stations2.get(x).getId();q++  ) {
    													rute[sum]=q;
    													sum++;
    												}
    												
    											}
    											
    											
    											
    										}
    										
    										
    										
    										else if(id1[0]<stations1.get(z).getId()) {
    											if(id2[0]>stations2.get(x).getId()) {
    												for(int p = stations1.get(xx).getId();p>id1[0];p--) {
    													rute[sum]=p;
    													sum++;
    												}
    												for(int q =id2[0];q >=stations2.get(x).getId();q--  ) {
    													rute[sum]=q;
    													sum++;
    												}
    												
    											}
    											else if(id2[0]<stations2.get(x).getId()) {
    												for(int p = stations1.get(xx).getId();p>id1[0];p--) {
    													rute[sum]=p;
    													sum++;
    												}
    												for(int q =id2[0];q <=stations2.get(x).getId();q++  ) {
    													rute[sum]=q;
    													sum++;
    												}
    												
    											}
    											
    												
    										}
    										break;
    									}
    									break;
    								}
    								
    							}
    							
    						}
    					
    					}
    					
            			
            			
            		}
        	
        	
        	int sumi=0;
        	for(int ii= 1;ii<rute.length;ii++) {
        		if(rute[ii]!=0) {
        			sumi=sumi+1;
        		}
        	}
        	 
        	req.setAttribute("rute",rute);
        	req.setAttribute("sumi",sumi);
    		req.getRequestDispatcher("list.jsp").forward(req,resp);
    	 }
    
    	}
      }
    }
    

      

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>查询页面</title>
    </head>
    <body>
    <%
    	     Object message = request.getAttribute("message");//放置一个字符串,并取出
    	     if(message!=null && !"".equals(message)){
    	 
    	%>
    	     <script type="text/javascript">
    	          alert("<%=request.getAttribute("message")%>");
    	     </script>
    	<%} %>
    	<div align="center">
    		<h1 style="color: black;">站点信息查询</h1>
    		<form action="StationServlet?method=chaxun" method="post" onsubmit="return check()">
    			
    				<p>始发站<input type="text" id="startname" name="startname"/>
    			
    			
    				<p>终点站<input type="text" id="endname" name="endname" />
    			
    			
    			
    				<p><button type="submit" class="b">查   询</button>
    			
    		</form>
    			<a href="ditu.jsp" >查看地铁线路图</a>
    	</div>
    
    	<script type="text/javascript">
    		function check() {
    			var startname = document.getElementById("startname");;
    			var endname = document.getElementById("endname");
    			
    			//非空
    			if(startname.value == '') {
    				alert('始发站不能为空');
    				startname.focus();
    				return false;
    			}
    			if(endname.value == '') {
    				alert('终点站不能为空');
    				endname.focus();
    				return false;
    			}
    				
    		}
    	</script>
    
    </body>
    </html>
    

      

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*" %>
    <jsp:useBean id="dao" class="Dao.Dao" scope="page"/>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>查询结果页面</title>
    </head>
    <body>
    <%
    	     Object message = request.getAttribute("message");
    	     if(message!=null && !"".equals(message)){
    	 
    	%>
    	     <script type="text/javascript">
    	          alert("<%=request.getAttribute("message")%>");
    	     </script>
    	<%} %>
    	<div align="center">
    		<h1 style="color: black;">站点信息列表</h1>
    		<a href="index.jsp">返回查询界面</a>
    		<h3 style="color: black;">共${sumi}站</h3>
    		<table class="tb">
    				<tr>
    					<td>站点编号</td>
    					<td>站点名称</td>
    				</tr>
    		<%
    		int[] ids = (int[]) request.getAttribute("rute");
    		for(int i = 0 ; i < ids.length;i++){
    			ResultSet rs = dao.getStationById(ids[i]);
    			if(rs==null){
    	        	 %>
    	             <tr align="center" valign="middle"><td colspan="4">没有记录显示!</td>
    	             </tr>
    	             <%
    	        }
    	        
    			
    		      while(rs.next()){
    		      %>
    		      <tr align="center" valign="middle" height="22">
    		        	
    		        <td><%=rs.getInt("id") %></td>  		 
    		        <td><%=rs.getString("name") %></td>                 	
    		      </tr>
    		      <%
    		      }
    		}
    				%>
    				
    		
            
            
    			
    			
    				
    		
    		</table>
    	</div>
    </body>
    </html>
    

      代码运行截图:

    无需换乘:鹿泉中心---柏林路

    需要换乘:

     

  • 相关阅读:
    Linux环境下安装MySQL(解压方式)
    Linux环境下安装MySQL(yum方式)
    Linux环境下安装ActiveMq
    Linux环境下安装Tomcat
    Linux环境下安装JDK
    Linux就该这么学--命令集合6(打包压缩文件、文件查询搜索命令)
    Linux就该这么学--命令集合5(用户与组管理命令)
    Linux就该这么学--命令集合4(文件目录管理命令)
    Linux就该这么学--命令集合3(文本文件编辑命令)
    解决shell脚本“syntax error near unexpected token `fi'”的问题。
  • 原文地址:https://www.cnblogs.com/zjl-0217/p/10650266.html
Copyright © 2020-2023  润新知